digitalmars.D.learn - getopt example please
- Andrew Edwards (7/7) Feb 26 2014 Request a small example of how to use getopt to accomplish the following...
- Tobias Pankrath (9/19) Feb 26 2014 This is not fully automated with getopt. You could use getopt to
- simendsjo (5/21) Feb 26 2014 args[0] is the program itself. As Tobias mention, you can check the
- Vladimir Panteleev (40/48) Feb 26 2014 import std.getopt;
- Andrew Edwards (2/46) Feb 26 2014 Thank you very much.
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
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, AndrewThis 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
On 02/26/2014 11:06 AM, Tobias Pankrath wrote:On Wednesday, 26 February 2014 at 09:57:19 UTC, Andrew Edwards wrote: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.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, AndrewThis 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
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 optionsimport 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
On 2/26/14, 5:37 AM, Vladimir Panteleev wrote:On Wednesday, 26 February 2014 at 09:57:19 UTC, Andrew Edwards wrote:Thank you very much.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 optionsimport 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