From ed6500ca207efdf9ff2d7ff54c5222211dace608 Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Wed, 25 Aug 2021 03:13:25 -0500 Subject: [PATCH 1/2] docs: add a quick example to the docs --- README.md | 46 +++++++++++++++++++++++++++++++++++++++++++++- quick_example.d | 28 ++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+), 1 deletion(-) create mode 100644 quick_example.d diff --git a/README.md b/README.md index bb29a61..53e22e8 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,50 @@ A command line and config file parser for DLang +## Quick Example +```d +import args : Arg, Optional, parseArgsWithConfigFile, printArgsHelp; + +static struct MyOptions +{ + @Arg("the input file", Optional.yes) string inputFilename; + @Arg("test values", 't') int[] testValues; + @Arg("Enable feature") bool enableFeature; +} + +MyOptions getOptions(ref string[] args) +{ + MyOptions options; + + bool helpWanted = parseArgsWithConfigFile(options, args); + + if (helpWanted) + { + printArgsHelp(options, "A text explaining the program"); + } + return options; +} + +void main(string[] args) +{ + const options = getOptions(args); // or args.dup to keep the original args + + // use options here.... +} +``` + +This gives: +```ps1 +❯ ./quick_example --help +A text explaining the program + --inputFilename Type: dchar[] default: Help: the input file +-t --testValues Type: int[] default: [] Help: test values + --enableFeature Type: bool default: false Help: Enable feature + +``` + +## Explanation + `argsd` arguments are structures as shown below. Each argument that should be searched for needs to have `@Arg()` attached to it. @@ -25,7 +69,7 @@ enum`s. All arguments take the shape "name value". Equal sign syntax is not supported. -Array values can be given as separate values of as comma separated values. +Array values can be given as a comma separated list. The name of the argument will be derived from the name of the member in the struct. The names are case sensitive. diff --git a/quick_example.d b/quick_example.d new file mode 100644 index 0000000..98d3a1f --- /dev/null +++ b/quick_example.d @@ -0,0 +1,28 @@ +import args : Arg, Optional, parseArgsWithConfigFile, printArgsHelp; + +static struct MyOptions +{ + @Arg("the input file", Optional.yes) string inputFilename; + @Arg("test values", 't') int[] testValues; + @Arg("Enable feature") bool enableFeature; +} + +MyOptions getOptions(ref string[] args) +{ + MyOptions options; + + bool helpWanted = parseArgsWithConfigFile(options, args); + + if (helpWanted) + { + printArgsHelp(options, "A text explaining the program"); + } + return options; +} + +void main(string[] args) +{ + const options = getOptions(args); // or args.dup to keep the original args + + // use options here.... +} From cd3dd7fb63a390a03df1adcd289562aa0572eb3a Mon Sep 17 00:00:00 2001 From: Amin Yahyaabadi Date: Wed, 25 Aug 2021 03:24:10 -0500 Subject: [PATCH 2/2] chore: use stroustrup brace style --- README.md | 12 ++++-------- quick_example.d | 12 ++++-------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/README.md b/README.md index 53e22e8..ca0a891 100644 --- a/README.md +++ b/README.md @@ -8,28 +8,24 @@ A command line and config file parser for DLang ```d import args : Arg, Optional, parseArgsWithConfigFile, printArgsHelp; -static struct MyOptions -{ +static struct MyOptions { @Arg("the input file", Optional.yes) string inputFilename; @Arg("test values", 't') int[] testValues; @Arg("Enable feature") bool enableFeature; } -MyOptions getOptions(ref string[] args) -{ +MyOptions getOptions(ref string[] args) { MyOptions options; bool helpWanted = parseArgsWithConfigFile(options, args); - if (helpWanted) - { + if (helpWanted) { printArgsHelp(options, "A text explaining the program"); } return options; } -void main(string[] args) -{ +void main(string[] args) { const options = getOptions(args); // or args.dup to keep the original args // use options here.... diff --git a/quick_example.d b/quick_example.d index 98d3a1f..5779252 100644 --- a/quick_example.d +++ b/quick_example.d @@ -1,27 +1,23 @@ import args : Arg, Optional, parseArgsWithConfigFile, printArgsHelp; -static struct MyOptions -{ +static struct MyOptions { @Arg("the input file", Optional.yes) string inputFilename; @Arg("test values", 't') int[] testValues; @Arg("Enable feature") bool enableFeature; } -MyOptions getOptions(ref string[] args) -{ +MyOptions getOptions(ref string[] args) { MyOptions options; bool helpWanted = parseArgsWithConfigFile(options, args); - if (helpWanted) - { + if (helpWanted) { printArgsHelp(options, "A text explaining the program"); } return options; } -void main(string[] args) -{ +void main(string[] args) { const options = getOptions(args); // or args.dup to keep the original args // use options here....