commandline
commandline copied to clipboard
Mix verbs with options
Is it possible to do something like the following?
myapp --interactive myapp --service myapp import --file C:\test.xml --model ModelOne
So I want the option of using the import verb, if one is supplied, --file and --model are required. If not, then check if --interactive or --service were supplied.
I tried something like the following
class Options
{
[Option('i'...
public bool Interactive { get; set; }
[Option('s'...
public bool Service { get; set; }
public ImportOptions Import { get; set; }
[Verb("import"...
public class ImportOptions
{
...
}
}
I am using 2.1.1-beta on .net standard.
This is possible by making "interactive" and "service" verbs. Make the target classes empty classes, and add the double dash to the verb name:
class Options
{
[Verb("--interactive")]
public class Interactive
{
}
[Verb("--service")]
public class Service
{
}
[Verb("import")]
public class ImportOptions
{
[Option("file", Required = true)]
public string File { get; set; }
[Option("model", Required = true)]
public string Model { get; set; }
}
}
class Program
{
static void Main(string[] args)
{
CommandLine.Parser.Default.ParseArguments<Options.Interactive, Options.Service, Options.ImportOptions>(args)
.WithParsed<Options.Interactive>(opt => Console.WriteLine("Interactive mode"))
.WithParsed<Options.Service>(opt => Console.WriteLine("Service mode"))
.WithParsed<Options.ImportOptions>(opt => Console.WriteLine($"Import mode: {opt.File} - {opt.Model}"));
}
}
Hi all, if possible I'd suggest to make interactive and service normal verbs (without dashes), if possible.
At the moment when using verbs none global switch is supported, but maybe in future.
Thanks for posting. :)