/// <summary> /// Determine if the arguments can be accepted. /// </summary> /// <param name="rawArgs">Arguments from commandline.</param> /// <param name="prefix">-</param> /// <param name="separator">:</param> /// <returns>True if success. False otherwise.</returns> public virtual BoolMessageItem <Args> AcceptArgs(string[] rawArgs, string prefix, string separator) { Settings.ArgsPrefix = prefix; Settings.ArgsSeparator = separator; // If there is an argument reciever, then reset the _args.Schema // using the arg attributes in the reciever. if (IsArgumentRecieverApplicable) { _args.Schema.Items = ArgsHelper.GetArgsFromReciever(Settings.ArgsReciever); } Args args = new Args(rawArgs, Options); // Are the args required? if (args.IsEmpty && !Settings.ArgsRequired) { return(new BoolMessageItem <Args>(args, true, "Arguments not required.")); } // Handle -help, -about, -pause. BoolMessageItem <Args> optionsResult = AppHelper.HandleOptions(this, args); if (!optionsResult.Success) { return(optionsResult); } // Validate/Parse args. BoolMessageItem <Args> result = Args.Accept(rawArgs, prefix, separator, 1, Options, OptionsExamples); // Successful ? Apply args to object reciever if (result.Success) { // Store the parsed args. _args = result.Item; if (IsArgumentRecieverApplicable) { Args.Accept(rawArgs, prefix, separator, Settings.ArgsReciever); } } // Errors ? Show them. if (!result.Success) { ArgsUsage.ShowError(result.Message); } return(result); }
/// <summary> /// Show how to parse the command line args. /// </summary> /// <returns></returns> public bool AcceptArguments() { // Show the arguments. Logger.Info(ArgsUsage.Build(_argsReciever)); // If args not supplied. simulate them. if (_args == null || _args.Length == 0) { _args = new string[] { "-env:Prod", "-date:${today}", "-dryrun:false", "MySettings", "8" } } ; return(Args.Accept(_args, "-", ":", _argsReciever)); }
/// <summary> /// Run the application. /// </summary> public override BoolMessageItem Execute() { //<doc:example> Args.InitServices((textargs) => ComLib.LexArgs.ParseList(textargs), (arg) => ComLib.Subs.Substitutor.Substitute(arg)); // Sample raw command line args. string[] rawArgs = new string[] { "-config:prod.xml", "-date:${t-1}", "-readonly:true", "myApplicationId" }; // Option 1. Statically parse using -, : as prefix/separator. Args args = Args.Parse(rawArgs, "-", ":").Item; Console.WriteLine("Config : {0}, BusinessDate : {1}, [0] : {2}", args.Named["config"], args.Named["date"], args.Positional[0]); // Option 2. Statically parse args and apply them on an object. StartupArgs reciever = new StartupArgs(); bool accepted = Args.Accept(rawArgs, "-", ":", reciever); Console.WriteLine(string.Format("Accepted config : {0}, date : {1}, readonly : {2}, settingsId: {3}", reciever.Config, reciever.BusinessDate, reciever.ReadonlyMode, reciever.DefaultSettingsId)); // Option 3: Instance based parsing with Fluent-like Schema population. Args args2 = new Args("-", ":").Schema.AddNamed <string>("config").Required.DefaultsTo("dev.xml").Describe("Configuration file") .AddNamed <bool>("readonly").Required.DefaultsTo(false).Describe("Run app in readonly mode") .AddNamed <DateTime>("date").Required.DefaultsTo(DateTime.Today).Interpret.Describe("Business date").Examples("${t-1}", "${today} | ${t-1}") .AddPositional <int>(0).Optional.Describe("Application Id").Args; args2.DoParse(rawArgs); // Check for -help, -version -info rawArgs = new string[] { "-help" }; Args args3 = new Args(rawArgs, "-", ":"); if (args3.IsHelp) { // Usage Option 1. Show usage of the arguments. Console.WriteLine(args2.GetUsage("My Sample Application")); // Usage Option 2. Display usage using reciever. // ( NOTE: -help is automatically interpreted to display args usage). ArgsUsage.ShowUsingReciever(Settings.ArgsReciever, Settings.ArgsPrefix, Settings.ArgsSeparator); } //</doc:example> return(BoolMessageItem.True); }
/// <summary> /// Run the application. /// </summary> public override BoolMessageItem Execute() { // Sample command line args. string[] argList = new string[] { "-config:prod.xml", "-date:${t-1}", "-readonly:true", "myApplicationId" }; // Option 1. Parse using -, : as prefix/separator. Args args = Args.Parse(argList, "-", ":").Item; Console.WriteLine("Config : {0}, BusinessDate : {1}, [0] : {2}", args.Named["config"], args.Named["date"], args.Positional[0]); // Option 2. Parse args and apply them on an object. StartupArgs reciever = new StartupArgs(); bool accepted = Args.Accept(argList, "-", ":", reciever); Console.WriteLine(string.Format("Accepted config : {0}, date : {1}, readonly : {2}, settingsId: {3}", reciever.Config, reciever.BusinessDate, reciever.ReadonlyMode, reciever.DefaultSettingsId)); // Display -help text. // ( NOTE: -help is automatically interpreted to display args usage). ArgsUsage.ShowUsingReciever(Settings.ArgsReciever); return(BoolMessageItem.True); }