Esempio n. 1
0
 /// <summary>
 ///     A generic extension method to automatically process command line parameters and provide a strongly-typed form to a
 ///     specified processing function
 /// </summary>
 /// <typeparam name="TCallSite">
 ///     The type of the program which needs to process command-line arguments. Typically this class has a function of the
 ///     form
 ///     <code>
 ///         internal static int Main(string[] args)
 ///         {
 ///         ...
 ///         }
 ///     </code>
 /// </typeparam>
 /// <typeparam name="TCommandLineParams"> The strongly typed container which specifies the expected command-line parameters </typeparam>
 /// <param name="_this"> The running instance of the program class. Used as the 'this' parameter to this method </param>
 /// <param name="args"> The array of command-line arguments as passed in from the environment </param>
 /// <param name="action">
 ///     The actual function which does the work to be done inside Main, but with reference to strongly-typed parameters
 ///     instead of <paramref name="args" />
 /// </param>
 /// <example>
 ///     Consider a hypothetical app which requires a few command-line parameters, some optional, and some flags: We can
 ///     create a type to encapsulate the arguments, and implement the Main function as follows:
 ///     <code>
 ///       [CommandLineApplication("A funky application to create the zero-th version of something")]
 ///       internal class Parameters
 ///       {
 ///           [CommandLineArgument("database", "The full path and name of the database")]
 ///           public string DatabasePath { get; set; }
 ///
 ///           [CommandLineArgument("clobber", "Should this database be clobbered", IsFlag = true)]
 ///           public bool Clobber { get; set; }
 ///
 ///           [CommandLineArgument("create", "Should this database be created", true)]
 ///           public bool Create { get; set; }
 ///       }
 ///
 ///       internal class Program
 ///       {
 ///           internal static void Main(string[] args)
 ///           {
 ///               (new Program()).Run&lt;Program,Parameters&gt;(args, _ => RunAction(_)) ;
 ///           }
 ///
 ///           internal static void RunAction(Parameters parameters)
 ///           {
 ///               Console.WriteLine(parameters.DatabasePath);
 ///               return 0;
 ///           }
 ///       }
 ///  </code>
 ///     Running this:
 ///     C:/&gt; runprog --database="foo.sdb" foo.sdb
 ///     Press any key to continue . . .
 ///     Further, we get two arguments for free:
 ///     * --help will print the usage parameters and quit
 ///     * --verbose will print the effective parameters used for execution, and perform the execution
 ///     C:/&gt; runprog --verbose --database="foo.sdb" --clobber
 ///     ***********************************************************************************************
 ///     ***
 ///     *** runprog.exe - A funky application to create the zero-th version of something
 ///     ***
 ///     *** Run Started: 15/07/2011 8:05:20 PM
 ///     ***
 ///     *** The effective value of --help is [null]
 ///     *** The effective value of --verbose is [null]
 ///     *** The effective value of --database is [foo.sdb]
 ///     *** The effective value of --clobber is [True]
 ///     *** The effective value of --create is [True]
 ///     ***
 ///     ***********************************************************************************************
 ///     Press any key to continue . . .
 /// </example>
 public static void Run <TCallSite, TCommandLineParams>(
     this TCallSite _this,
     string[] args,
     Action <TCommandLineParams> action = null) where TCallSite : class
 {
     try {
         Run(args, action);
     }
     catch (CommandLineParameterException commandLineParameterException)
     {
         CommandLineApplicationClosure <TCommandLineParams> .Error(
             commandLineParameterException,
             "Validation failed",
             -1);
     }
 }
Esempio n. 2
0
        /// <summary>
        ///     A generic extension method to automatically process command line parameters and provide a strongly-typed form to a
        ///     specified processing function
        /// </summary>
        /// <typeparam name="TCallSite">
        ///     The type of the program which needs to process command-line arguments. Typically this class has a function of the
        ///     form
        ///     <code>
        ///         internal static int Main(string[] args)
        ///         {
        ///         ...
        ///         }
        ///     </code>
        /// </typeparam>
        /// <typeparam name="TCommandLineParams"> The strongly typed container which specifies the expected command-line parameters </typeparam>
        /// <param name="_this"> The running instance of the program class. Used as the 'this' parameter to this method </param>
        /// <param name="args"> The array of command-line arguments as passed in from the environment </param>
        /// <param name="action">
        ///     The actual function which does the work to be done inside Main, but with reference to strongly-typed parameters
        ///     instead of <paramref name="args" />
        /// </param>
        /// <returns> The result value of the actual function provided. </returns>
        /// <example>
        ///     Consider a hypothetical app which requires a few command-line parameters, some optional, and some flags: We can
        ///     create a type to encapsulate the arguments, and implement the Main function as follows:
        ///     <code>
        ///       [CommandLineApplication("A funky application to create the zero-th version of something")]
        ///       internal class Parameters
        ///       {
        ///           [CommandLineArgument("database", "The full path and name of the database")]
        ///           public string DatabasePath { get; set; }
        ///
        ///           [CommandLineArgument("clobber", "Should this database be clobbered", IsFlag = true)]
        ///           public bool Clobber { get; set; }
        ///
        ///           [CommandLineArgument("create", "Should this database be created", true)]
        ///           public bool Create { get; set; }
        ///       }
        ///
        ///       internal class Program
        ///       {
        ///           internal static int Main(string[] args)
        ///           {
        ///               return (new Program()).Run&lt;Program,Parameters&gt;(args, _ => RunAction(_)) ;
        ///           }
        ///
        ///           internal static int RunAction(Parameters parameters)
        ///           {
        ///               Console.WriteLine(parameters.DatabasePath);
        ///               return 0;
        ///           }
        ///       }
        ///  </code>
        ///     Running this:
        ///     C:/&gt; runprog --database="foo.sdb" foo.sdb
        ///     Press any key to continue . . .
        ///     Further, we get two arguments for free:
        ///     * --help will print the usage parameters and quit
        ///     * --verbose will print the effective parameters used for execution, and perform the execution
        ///     C:/&gt; runprog --verbose --database="foo.sdb" --clobber
        ///     ***********************************************************************************************
        ///     ***
        ///     *** runprog.exe - A funky application to create the zero-th version of something
        ///     ***
        ///     *** Run Started: 15/07/2011 8:05:20 PM
        ///     ***
        ///     *** The effective value of --help is [null]
        ///     *** The effective value of --verbose is [null]
        ///     *** The effective value of --database is [foo.sdb]
        ///     *** The effective value of --clobber is [True]
        ///     *** The effective value of --create is [True]
        ///     ***
        ///     ***********************************************************************************************
        ///     Press any key to continue . . .
        /// </example>
        public static int Run <TCallSite, TCommandLineParams>(
            this TCallSite _this,
            string[] args,
            Func <TCommandLineParams, int> action = null) where TCallSite : class
        {
            try {
                return(Run(args, action));
            }
            catch (CommandLineParameterException commandLineParameterException)
            {
                CommandLineApplicationClosure <TCommandLineParams> .Error(
                    commandLineParameterException,
                    "Validation failed",
                    -1);

                // should never reach this, because Usage() calls Environment.Exit()
                throw new Exception("Should never reach here. Usage() changed to not call Environment.Exit?");
            }
        }
Esempio n. 3
0
        /// <summary>
        ///     A generic extension method to automatically process command line parameters and provide a strongly-typed form to a
        ///     specified processing method
        /// </summary>
        /// <typeparam name="TCommandLineParams"> The strongly typed container which specifies the expected command-line parameters </typeparam>
        /// <param name="args"> The array of command-line arguments as passed in from the environment </param>
        /// <param name="action">
        ///     The actual function which does the work to be done inside Main, but with reference to strongly-typed parameters
        ///     instead of
        ///     <paramref
        ///         name="args" />
        /// </param>
        /// <example>
        ///     Consider a hypothetical app which requires a few command-line parameters, some optional, and some flags: We can
        ///     create a type to encapsulate the arguments, and implement the Main function as follows:
        ///     <code>
        ///       [CommandLineApplication("A funky application to create the zero-th version of something")]
        ///       internal class Parameters
        ///       {
        ///           [CommandLineArgument("database", "The full path and name of the database")]
        ///           public string DatabasePath { get; set; }
        ///
        ///           [CommandLineArgument("clobber", "Should this database be clobbered", IsFlag = true)]
        ///           public bool Clobber { get; set; }
        ///
        ///           [CommandLineArgument("create", "Should this database be created", true)]
        ///           public bool Create { get; set; }
        ///       }
        ///
        ///       internal class Program
        ///       {
        ///           internal static void Main(string[] args)
        ///           {
        ///               (new Program()).Run&lt;Program,Parameters&gt;(args, _ => RunAction(_)) ;
        ///           }
        ///
        ///           internal static void RunAction(Parameters parameters)
        ///           {
        ///               Console.WriteLine(parameters.DatabasePath);
        ///               return 0;
        ///           }
        ///       }
        ///  </code>
        ///     Running this:
        ///     C:/&gt; runprog --database="foo.sdb" foo.sdb
        ///     Press any key to continue . . .
        ///     Further, we get two arguments for free:
        ///     * --help will print the usage parameters and quit
        ///     * --verbose will print the effective parameters used for execution, and perform the execution
        ///     C:/&gt; runprog --verbose --database="foo.sdb" --clobber
        ///     ***********************************************************************************************
        ///     ***
        ///     *** runprog.exe - A funky application to create the zero-th version of something
        ///     ***
        ///     *** Run Started: 15/07/2011 8:05:20 PM
        ///     ***
        ///     *** The effective value of --help is [null]
        ///     *** The effective value of --verbose is [null]
        ///     *** The effective value of --database is [foo.sdb]
        ///     *** The effective value of --clobber is [True]
        ///     *** The effective value of --create is [True]
        ///     ***
        ///     ***********************************************************************************************
        ///     Press any key to continue . . .
        /// </example>
        public static void Run <TCommandLineParams>(this string[] args, Action <TCommandLineParams> action = null)
        {
            try
            {
                action = action ?? (_ => { });

                var _params = CommandLineApplicationClosure <TCommandLineParams> .ProcessArguments(args);

                action(_params);
            }
            catch (CommandLineParameterException commandLineParameterException)
            {
                CommandLineApplicationClosure <TCommandLineParams> .Error(
                    commandLineParameterException,
                    "Validation failed",
                    -1);

                // should never reach this, because Usage() calls Environment.Exit()
                throw new Exception("Should never reach here. Usage() changed to not call Environment.Exit?");
            }
        }