Skip to content

Commit 06dc836

Browse files
Add public help API
1 parent 5f13007 commit 06dc836

File tree

14 files changed

+1151
-715
lines changed

14 files changed

+1151
-715
lines changed

docs/ad.tree

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@
3131
<toc-element topic="ansiStylingArgument.md"/>
3232
<toc-element topic="CLI-API.md"/>
3333
<toc-element topic="Config.md"/>
34+
<toc-element toc-title="Help">
35+
<toc-element topic="HelpScreen.md"/>
36+
<toc-element topic="HelpPrinter.md"/>
37+
<toc-element topic="...HelpInfo.md"/>
38+
</toc-element>
3439
<toc-element topic="Param-RawParam.md"/>
3540
<toc-element topic="Result.md"/>
3641
<toc-element topic="Style.md"/>
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import argparse;
2+
3+
struct T
4+
{
5+
string a;
6+
}
7+
8+
enum Config cfg = {
9+
helpPrinter:
10+
(Style style, CommandHelpInfo[] cmds)
11+
{
12+
import std.stdio : stderr;
13+
scope auto output = stderr.lockingTextWriter();
14+
15+
new HelpPrinter(style).printHelp(_ => output.put(_), cmds);
16+
}
17+
};
18+
19+
T t;
20+
assert(!CLI!(cfg, T).parseArgs(t, ["-h"]));
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
# ...HelpInfo
2+
3+
There are few structs that describe help information for argument parsing entities.
4+
5+
## ArgumentHelpInfo
6+
7+
`ArgumentHelpInfo` contains data that is needed to print help for an command line argument:
8+
9+
- `const string[] names` - all short and long names of an argument prefixed with `Config.shortNamePrefix` or
10+
`Config.shortNamePrefix` respectively.
11+
- `string description` - description of an argument.
12+
- `string placeholder` - value placeholder (eg. `VALUE` for `--foo VALUE` argument).
13+
- `bool multipleOccurrence` - flag denoting that an argument can appear multiple times in command line.
14+
- `bool optionalValue` - flag denoting that the value is optional for argument.
15+
- `bool optionalArgument` - flag denoting that an argument itself is optional.
16+
- `bool positional` - flag denoting that an argument is positional (otherwise, it's named argument).
17+
- `bool hidden` - flag denoting that an argument should be hidden from help screen.
18+
- `bool booleanFlag` - flag denoting that an argument is a boolean flag.
19+
20+
## ArgumentGroupHelpInfo
21+
22+
`ArgumentGroupHelpInfo` contains information about arguments group:
23+
24+
- `string name` - name of the group.
25+
- `string description` - group description.
26+
- `const size_t[] argIndex` - indexes of arguments that belong to this group. Arguments themselves are store in [`CommandHelpInfo`](#commandhelpinfo).
27+
28+
## SubCommandHelpInfo
29+
30+
`SubCommandHelpInfo` contains information about subcommand:
31+
32+
- `const string[] names` - subcommand names.
33+
- `string description` - subcommand description.
34+
35+
## CommandHelpInfo
36+
37+
`CommandHelpInfo` contains information about subcommand:
38+
39+
- `string name` - command name.
40+
- `string usage` - command usage.
41+
- `string description` - command description.
42+
- `string epilog` - command epilog.
43+
- `ArgumentHelpInfo[] arguments` - all arguments of the command.
44+
- `ArgumentGroupHelpInfo requiredGroup` - group of required arguments of the command.
45+
- `ArgumentGroupHelpInfo optionalGroup` - group of optional arguments of the command.
46+
- `ArgumentGroupHelpInfo[] userGroups` - user-defined groups of arguments of the command.
47+
- `SubCommandHelpInfo[] subCommands` - subcommands.
48+
49+
This struct also has the following helpers to iterate over positional or named arguments only - the both return filtered
50+
range of arguments (`ArgumentHelpInfo` objects):
51+
52+
- `auto positionalArguments() const`
53+
- `auto namedArguments() const`

docs/topics/reference/Config.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,20 @@ Help text from the first part of the example code above:
168168

169169
<img src="config_styling.png" alt="Config styling example" border-effect="rounded"/>
170170

171+
## Help printer {id="helpPrinter"}
172+
173+
`Config.helpPrinter` is a handler function to print help screen.
174+
It receives two parameters:
175+
- `Style style` - style that should be applied to help screen.
176+
- `CommandHelpInfo[] cmds` - current stack of (sub)commands starting with top-level command.
177+
For example, if command line contains `tool subcmd1 subcmd2 -h` then `cmd` will contain array of `CommandHelpInfo`
178+
objects that corresponds to `tool`, `subcmd1`, `subcmd2` commands respectively.
179+
180+
Example:
181+
182+
<code-block src="code_snippets/config_helpPrinter.d" lang="c++"/>
183+
184+
171185
## Error handling {id="errorHandler"}
172186

173187
`Config.errorHandler` is a handler function for all errors occurred during command line parsing.

0 commit comments

Comments
 (0)