예제 #1
0
        public static void Main(string[] args)
        {
            ProgramArguments arguments = ProgramArguments.Create(args);

            // No need to do anything when the value is null; Create already printed errors and usage to the console
            if (arguments != null)
            {
                // This application doesn't do anything useful, it's just a sample of using CommandLineParser after all. We use reflection to print
                // the values of all the properties of the sample's CommandLineArguments class, which correspond to the sample's command line arguments.

                // We use the LineWrappingTextWriter to neatly wrap console output.
                using (LineWrappingTextWriter writer = LineWrappingTextWriter.ForConsoleOut())
                {
                    // Print the full command line as received by the application
                    writer.WriteLine("The command line was: {0}", Environment.CommandLine);
                    writer.WriteLine();
                    // Print the values of the arguments, using reflection to get all the property values
                    writer.WriteLine("The following argument values were provided:");
                    writer.WriteLine("Source: {0}", arguments.Source ?? "(null)");
                    writer.WriteLine("Destination: {0}", arguments.Destination ?? "(null)");
                    writer.WriteLine("Index: {0}", arguments.Index);
                    writer.WriteLine("Date: {0}", arguments.Date == null ? "(null)" : arguments.Date.ToString());
                    writer.WriteLine("Count: {0}", arguments.Count);
                    writer.WriteLine("Verbose: {0}", arguments.Verbose);
                    writer.WriteLine("Values: {0}", arguments.Values == null ? "(null)" : "{ " + string.Join(", ", arguments.Values) + " }");
                    writer.WriteLine("Help: {0}", arguments.Help);
                }
            }
        }
예제 #2
0
 public override void Run()
 {
     // This method is invoked after all command line arguments have been parsed
     try
     {
         // We use a LineWrappingTextWriter to neatly wrap console output
         using (LineWrappingTextWriter writer = LineWrappingTextWriter.ForConsoleOut())
             using (StreamReader reader = new StreamReader(FileName, Encoding.GetEncoding(EncodingName)))
             {
                 // Write the contents of the file to the console
                 string line;
                 while ((line = reader.ReadLine()) != null)
                 {
                     writer.WriteLine(line);
                 }
             }
     }
     catch (ArgumentException ex)  // Happens if the encoding name is invalid
     {
         Program.WriteErrorMessage(ex.Message);
         // The Main method will return the exit status to the operating system. The numbers are made up for the sample, they don't mean anything.
         // Usually, 0 means success, and any other value indicates an error.
         ExitCode = 2;
     }
     catch (IOException ex)
     {
         Program.WriteErrorMessage(ex.Message);
         ExitCode = 2;
     }
     catch (UnauthorizedAccessException ex)
     {
         Program.WriteErrorMessage(ex.Message);
         ExitCode = 2;
     }
 }