Closed andresv closed 9 years ago
Ok, I've added an example of how I usually accomplish the task: https://github.com/tailhook/rust-argparse/blob/master/examples/subcommands.rs
While I'd like to add some functionality that would simplify adding groups of options and subcommands, but I can't find out good API. The complex point is that it should allow keep different subcommands in different options, which is slightly hard to achieve with rust borrow rules.
Let me know if this solution is ok for you, or if you have another ideas.
Very clever, I will try that.
But what about --help? In this way subcommand arguments are not automatically added.
Tried subcommand.rs:
Usage:
./target/debug/doppler [OPTIONS] COMMAND [ARGUMENTS ...]
Plays or records sound
positional arguments:
command Command to run (either "play" or "record")
arguments Arguments for command
optional arguments:
-h,--help show this help message and exit
-v,--verbose Be verbose
But what about --help? In this way subcommand arguments are not automatically added.
Right, the --help
part is the complex one. You get help for subcommand by:
command subcommand --help
Which is reasonable enough, for most things.
The problematic part of making help for all subcommands on one page is incapsulation, you can't borrow variables from several functions at once. And I don't like lifting arguments out of the module which implements the functionality itself.
I switched to clap which has subcommand support built in: https://github.com/kbknapp/clap-rs. It has also other very nice features. Clap seems to be the most polished rust argparser so far.
It would be nice if argparse supports commands. For example lets say I have a application that has 2 operating modes: const and tracking. Each mode has its own args and some of them are required args. Here is an example where I tried to migrate my application from docopt to argparese. Argument shift should be available only for const mode. My approach was to use shift as type Option, however it seems to be quite difficult to implement it that way. Therefore currently I will use shift as u32 and init with 0xFFFFFFFF which means NA.
NB: maybe it is not that difficult and I just do not know the right way. Anyway here is a snippet what I tried to do: