Commandline: Mix verbs with options

Created on 4 Nov 2017  路  2Comments  路  Source: commandlineparser/commandline

Issue by UmairB
_Thursday Apr 27, 2017 at 20:53 GMT_
_Originally opened as https://github.com/gsscoder/commandline/issues/442_


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.

question

Most helpful comment

Comment by Scottz0r
_Saturday Apr 29, 2017 at 22:18 GMT_


This is possible by making "interactive" and "service" verbs. Make the target classes empty classes, and add the double dash to the verb name:

```c#
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(args)
.WithParsed(opt => Console.WriteLine("Interactive mode"))
.WithParsed(opt => Console.WriteLine("Service mode"))
.WithParsed(opt => Console.WriteLine($"Import mode: {opt.File} - {opt.Model}"));
}
}
```

All 2 comments

Comment by Scottz0r
_Saturday Apr 29, 2017 at 22:18 GMT_


This is possible by making "interactive" and "service" verbs. Make the target classes empty classes, and add the double dash to the verb name:

```c#
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(args)
.WithParsed(opt => Console.WriteLine("Interactive mode"))
.WithParsed(opt => Console.WriteLine("Service mode"))
.WithParsed(opt => Console.WriteLine($"Import mode: {opt.File} - {opt.Model}"));
}
}
```

Comment by gsscoder
_Friday May 12, 2017 at 06:49 GMT_


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. :)

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Eason-Lian picture Eason-Lian  路  3Comments

Valinwolf picture Valinwolf  路  3Comments

ericnewton76 picture ericnewton76  路  5Comments

SeanoNET picture SeanoNET  路  4Comments

Rohansi picture Rohansi  路  6Comments