www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - getopt example please

reply Andrew Edwards <ridimz yahoo.com> writes:
Request a small example of how to use getopt to accomplish the following:

[1] program subcommand //process subcommand with default arguments if any
[2] program -h subcommand //output help information about subcommand
[3] program subcommand --option1 --option2 true option3=log.txt // 
process subcommand with user specified options

Thanks,
Andrew
Feb 26 2014
next sibling parent reply "Tobias Pankrath" <tobias pankrath.net> writes:
On Wednesday, 26 February 2014 at 09:57:19 UTC, Andrew Edwards 
wrote:
 Request a small example of how to use getopt to accomplish the 
 following:

 [1] program subcommand //process subcommand with default 
 arguments if any
 [2] program -h subcommand //output help information about 
 subcommand
 [3] program subcommand --option1 --option2 true option3=log.txt 
 // process subcommand with user specified options

 Thanks,
 Andrew
This is not fully automated with getopt. You could use getopt to parse all non-positional arguments and than parse the rest by hand. -- getopt(args, ...); -- Now, args[1] (or [0]?) should be "subcommand".
Feb 26 2014
parent simendsjo <simendsjo gmail.com> writes:
On 02/26/2014 11:06 AM, Tobias Pankrath wrote:
 On Wednesday, 26 February 2014 at 09:57:19 UTC, Andrew Edwards wrote:
 Request a small example of how to use getopt to accomplish the following:

 [1] program subcommand //process subcommand with default arguments if any
 [2] program -h subcommand //output help information about subcommand
 [3] program subcommand --option1 --option2 true option3=log.txt //
 process subcommand with user specified options

 Thanks,
 Andrew
This is not fully automated with getopt. You could use getopt to parse all non-positional arguments and than parse the rest by hand. -- getopt(args, ...); -- Now, args[1] (or [0]?) should be "subcommand".
args[0] is the program itself. As Tobias mention, you can check the first subcommand and use a different getopt strategy for each. It's possible to say to getopt that it should not throw an error when it encounters unknown parameters so you can parse them yourself.
Feb 26 2014
prev sibling parent reply "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Wednesday, 26 February 2014 at 09:57:19 UTC, Andrew Edwards 
wrote:
 Request a small example of how to use getopt to accomplish the 
 following:

 [1] program subcommand //process subcommand with default 
 arguments if any
 [2] program -h subcommand //output help information about 
 subcommand
 [3] program subcommand --option1 --option2 true option3=log.txt 
 // process subcommand with user specified options
import std.getopt; void main(string[] args) { bool help; getopt(args, "h|help", &help, config.stopOnFirstNonOption, ); if (args.length == 1) { // Print general help and list of commands return; } auto subcommand = args[1]; switch (subcommand) { case "subcommand": { if (help) { // Print help for subcommand return; } bool option1; string option2, option3; getopt(args, "option1", &option1, "option2", &option2, "option3", &option3, ); // Process subcommand with specified options break; } default: throw new Exception("Unknown subcommand "~ subcommand); } }
Feb 26 2014
parent Andrew Edwards <ridimz yahoo.com> writes:
On 2/26/14, 5:37 AM, Vladimir Panteleev wrote:
 On Wednesday, 26 February 2014 at 09:57:19 UTC, Andrew Edwards wrote:
 Request a small example of how to use getopt to accomplish the following:

 [1] program subcommand //process subcommand with default arguments if any
 [2] program -h subcommand //output help information about subcommand
 [3] program subcommand --option1 --option2 true option3=log.txt //
 process subcommand with user specified options
import std.getopt; void main(string[] args) { bool help; getopt(args, "h|help", &help, config.stopOnFirstNonOption, ); if (args.length == 1) { // Print general help and list of commands return; } auto subcommand = args[1]; switch (subcommand) { case "subcommand": { if (help) { // Print help for subcommand return; } bool option1; string option2, option3; getopt(args, "option1", &option1, "option2", &option2, "option3", &option3, ); // Process subcommand with specified options break; } default: throw new Exception("Unknown subcommand "~ subcommand); } }
Thank you very much.
Feb 26 2014