/// <summary> /// Format a command line argument string from a parsed instance. /// </summary> /// <typeparam name="T">Type of <paramref name="options"/>.</typeparam> /// <param name="parser">Parser instance.</param> /// <param name="options">A parsed (or manually correctly constructed instance).</param> /// <returns>A string with command line arguments.</returns> public static string FormatCommandLine <T>(this Parser parser, T options) { return(parser.FormatCommandLine(options, config => { })); }
/// <summary> /// Format a command line argument string from a parsed instance in the form of string[]. /// </summary> /// <typeparam name="T">Type of <paramref name="options"/>.</typeparam> /// <param name="parser">Parser instance.</param> /// <param name="options">A parsed (or manually correctly constructed instance).</param> /// <returns>A string[] with command line arguments.</returns> public static string[] FormatCommandLineArgs <T>(this Parser parser, T options) { return(parser.FormatCommandLine(options, config => { }).SplitArgs()); }
} // proc AddToProcessEnvironment /// <summary>Eintrittspunkt in die Anwendung.</summary> /// <param name="args">Parameter die übergeben werden. Ungenutzt.</param> public static void Main(string[] args) { #if DEBUG var readlineAtTheEnd = false; #endif var printHeader = new Action(() => { Console.WriteLine(HeadingInfo.Default.ToString()); Console.WriteLine(CopyrightInfo.Default.ToString()); #if DEBUG readlineAtTheEnd = true; #endif }); try { var parser = new CommandLine.Parser(s => { s.CaseSensitive = false; s.IgnoreUnknownArguments = false; s.HelpWriter = null; s.ParsingCulture = CultureInfo.InvariantCulture; }); // work with arguments var r = parser.ParseArguments(args, new Type[] { typeof(RunOptions), typeof(RegisterOptions), typeof(UnregisterOptions) }); r.MapResult <RunOptions, RegisterOptions, UnregisterOptions, bool>( opts => // run { // print heading if (opts.Verbose) { printHeader(); } // validate arguments opts.Validate(); // execute the service var app = new DEServer(opts.ConfigurationFile, opts.Properties); if (opts.Verbose) // Run the console version of the service { app.ServiceLog = new ConsoleLog(); app.OnStart(); Console.WriteLine("Service is started."); Console.ReadLine(); app.OnStop(); } else { ServiceBase.Run(new Service(ServicePrefix + opts.ServiceName, app)); // Start as a windows service } return(true); }, opts => // register { // print heading printHeader(); // validate arguments opts.Validate(); // form the run command line var runOpts = new RunOptions(opts); var serviceCommandLine = parser.FormatCommandLine(runOpts, o => { o.PreferShortName = true; }); // register the service RegisterService(opts.ServiceName, serviceCommandLine); Console.WriteLine("Service '{0}{1}' created/modified.", ServicePrefix, opts.ServiceName); return(true); }, opts => // unregister { // print heading printHeader(); UnregisterService(opts.ServiceName); Console.WriteLine("Service '{0}{1}' removed.", ServicePrefix, opts.ServiceName); return(true); }, errors => { // print help var help = CommandLine.Text.HelpText.AutoBuild(r); if (errors.FirstOrDefault(e => e is HelpVerbRequestedError) != null) { help.AddPreOptionsLine(Environment.NewLine + "Usage:"); help.AddPreOptionsLine(" DEServer.exe run -v -c [configuration file] -n [name] {properties}"); help.AddPreOptionsLine(" DEServer.exe register -c [configuration file] -n [name] {properties}"); help.AddPreOptionsLine(" DEServer.exe unregister --name [name] "); } if (errors.FirstOrDefault(e => e is VersionRequestedError) != null) { // todo: add detailed version info help.AddPostOptionsLine("Assembly version: todo"); } Console.WriteLine(help.ToString()); return(false); } ); } catch (Exception e) { Console.WriteLine(e.GetMessageString()); #if DEBUG if (readlineAtTheEnd) { Console.ReadLine(); } #endif } } // proc Main
/// <summary> /// Format a command line argument string from a parsed instance. /// </summary> /// <typeparam name="T">Type of <see cref="options"/>.</typeparam> /// <param name="parser">Parser instance.</param> /// <param name="options">A parsed (or manually correctly constructed instance).</param> /// <returns>A string with command line arguments.</returns> public static string FormatCommandLine <T>(this Parser parser, T options) { return(parser.FormatCommandLine(options, config => config.NameStyleFormat = NameStyleFormat.PreferLongName)); }