|
| 1 | +# Optional Arguments |
| 2 | +Let's consider **optional arguments**. Optional arguments start with `-`, `--` or `/`, e.g., `-h`, `--verbose` or `/m`. |
| 3 | + |
| 4 | +## Table of Contents |
| 5 | +* [Flag Options](#flag-options) |
| 6 | + * [Help Option](#help-option) |
| 7 | + * [Version Option](#version-option) |
| 8 | +* [Value Options](#value-options) |
| 9 | + * [Multiple Value Options](#multiple-value-options) |
| 10 | + * [Enum Value Options](#enum-value-options) |
| 11 | +* [Custom Options](#custom-options) |
| 12 | +* [Option Groups](#option-groups) |
| 13 | + |
| 14 | +## Flag Options |
| 15 | +**Flag options** are options without value. They make working with boolean values easier. |
| 16 | + |
| 17 | +Here is an example of creating flag option and using it in the parser: |
| 18 | + |
| 19 | +```cs |
| 20 | +bool verbose = false; |
| 21 | + |
| 22 | +var option = new FlagOption("verbose", "v", |
| 23 | + description: "be verbose", |
| 24 | + afterHandlingAction: () => verbose = true); |
| 25 | + |
| 26 | +var parser = new ArgumentParser(); |
| 27 | +parser.AddOptions(option); |
| 28 | + |
| 29 | +parser.Parse(new string[] { "--verbose" }); |
| 30 | +// verbose: true |
| 31 | +``` |
| 32 | + |
| 33 | +### Help Option |
| 34 | +**Help option** is a special final flag option whose default action is to print the help and exit the program. |
| 35 | + |
| 36 | +This option is automatically added to the option set, so you don't have to add it explicitly: |
| 37 | + |
| 38 | +```cs |
| 39 | +var parser = new ArgumentParser(); |
| 40 | +parser.Parse(new string[] { "--help" }); |
| 41 | +``` |
| 42 | + |
| 43 | +You can disable automatic addition of the help option as follows: |
| 44 | + |
| 45 | +```cs |
| 46 | +var parser = new ArgumentParser() |
| 47 | +{ |
| 48 | + UseDefaultHelpOption = false |
| 49 | +}; |
| 50 | +``` |
| 51 | + |
| 52 | +Here is an example of creating custom help option and using it in the parser: |
| 53 | + |
| 54 | +```cs |
| 55 | +var parser = new ArgumentParser() |
| 56 | +{ |
| 57 | + UseDefaultHelpOption = false |
| 58 | +}; |
| 59 | + |
| 60 | +var option = new HelpOption("help", "h", |
| 61 | + description: "show command-line help", |
| 62 | + afterHandlingAction: () => |
| 63 | + { |
| 64 | + Console.WriteLine(parser.GenerateProgramDescription()); |
| 65 | + Environment.Exit(0); |
| 66 | + }); |
| 67 | + |
| 68 | +parser.AddOptions(option); |
| 69 | +parser.Parse(new string[] { "--help" }); |
| 70 | +``` |
| 71 | + |
| 72 | +### Version Option |
| 73 | +**Version option** is a special final flag option whose default action is to print the version information and exit the program. |
| 74 | + |
| 75 | +This option is automatically added to the option set, so you don't have to add it explicitly: |
| 76 | + |
| 77 | +```cs |
| 78 | +var parser = new ArgumentParser(); |
| 79 | +parser.Parse(new string[] { "--version" }); |
| 80 | +``` |
| 81 | + |
| 82 | +You can disable automatic addition of the help option as follows: |
| 83 | + |
| 84 | +```cs |
| 85 | +var parser = new ArgumentParser() |
| 86 | +{ |
| 87 | + UseDefaultVersionOption = false |
| 88 | +}; |
| 89 | +``` |
| 90 | + |
| 91 | +Here is an example of creating custom version option and using it in the parser: |
| 92 | + |
| 93 | +```cs |
| 94 | +var parser = new ArgumentParser() |
| 95 | +{ |
| 96 | + UseDefaultVersionOption = false |
| 97 | +}; |
| 98 | + |
| 99 | +var option = new VersionOption("version", "v", |
| 100 | + description: "show version information", |
| 101 | + afterHandlingAction: () => |
| 102 | + { |
| 103 | + Console.WriteLine(parser.ProgramVersion); |
| 104 | + Environment.Exit(0); |
| 105 | + }); |
| 106 | + |
| 107 | +parser.AddOptions(option); |
| 108 | +parser.Parse(new string[] { "--version" }); |
| 109 | +``` |
| 110 | + |
| 111 | +## Value Options |
| 112 | +**Value options** are options with value. The value type can be anything. However, for types that don't have a default converter, you will need to add a [custom converter](CustomConverters.md). |
| 113 | + |
| 114 | +Here is an example of creating value option and using it in the parser: |
| 115 | + |
| 116 | +```cs |
| 117 | +int? angle = null; |
| 118 | + |
| 119 | +var option = new ValueOption<int>("angle", "a", |
| 120 | + description: "angle by which you want to rotate the image", |
| 121 | + afterValueParsingAction: t => angle = t); |
| 122 | + |
| 123 | +var parser = new ArgumentParser(); |
| 124 | +parser.AddOptions(option); |
| 125 | + |
| 126 | +parser.Parse(new string[] { "--angle", "45" }); |
| 127 | +// angle: 45 |
| 128 | +``` |
| 129 | + |
| 130 | +### Multiple Value Options |
| 131 | +**Multiple value options** are options whose value is a list. The single value type can be anything. However, for types that don't have a default converter, you will need to add a [custom converter](CustomConverters.md). |
| 132 | + |
| 133 | +Here is an example of creating multiple value option and using it in the parser: |
| 134 | + |
| 135 | +```cs |
| 136 | +List<string> inputFiles = []; |
| 137 | + |
| 138 | +var option = new MultipleValueOption<string>("input", "i", |
| 139 | + description: "files that need to be processed", |
| 140 | + contextCapture: new OneOrMoreContextCapture(), |
| 141 | + afterValueParsingAction: t => inputFiles = new List<string>(t)); |
| 142 | + |
| 143 | +var parser = new ArgumentParser(); |
| 144 | +parser.AddOptions(option); |
| 145 | + |
| 146 | +parser.Parse(new string[] { "-i", "file0.txt", "file1.txt" }); |
| 147 | +// inputFiles: [file0.txt, file1.txt] |
| 148 | +``` |
| 149 | + |
| 150 | +One of the important features of the multiple value option is context capture. You can find out more about it in the [corresponding](OptionalArgumentsConfig.md#context-capture) section of the documentation. |
| 151 | + |
| 152 | +### Enum Value Options |
| 153 | +**Enum value options** are options whose value is enum. The value type can be any enum. |
| 154 | + |
| 155 | +Here is an example of creating enum value option and using it in the parser: |
| 156 | + |
| 157 | +```cs |
| 158 | +StringSplitOptions? splitOption = null; |
| 159 | + |
| 160 | +var option = new EnumValueOption<StringSplitOptions>("split-option", string.Empty, |
| 161 | + description: "specifies how the String.Split method should split a string", |
| 162 | + afterValueParsingAction: t => splitOption = t) |
| 163 | + |
| 164 | +var parser = new ArgumentParser(); |
| 165 | +parser.AddOptions(option); |
| 166 | + |
| 167 | +parser.Parse(new string[] { "--split-option", "TrimEntries" }); |
| 168 | +// splitOption: StringSplitOptions.TrimEntries |
| 169 | +``` |
| 170 | + |
| 171 | +## Custom Options |
| 172 | +You can create your own options. To do this you need to inherit your class from the `ICommonOption` interface. You can also use an existing option class as a base class. See examples of this kind of inheritance, for example, by looking at the implementation of the `FlagOption` and `EnumValueOption` classes. |
| 173 | + |
| 174 | +## Option Groups |
| 175 | +Options can be divided into groups. This division may be useful when there are a lot of options. |
| 176 | + |
| 177 | +```cs |
| 178 | +var parser = new ArgumentParser(); |
| 179 | +parser.DefaultGroup.Header = "Default group:"; |
| 180 | + |
| 181 | +var option = new ValueOption<int>("angle"); |
| 182 | +var additionalOption = new FlagOption("verbose"); |
| 183 | + |
| 184 | +parser.AddOptions(option); |
| 185 | + |
| 186 | +OptionGroup<ICommonOption> group = parser.AddOptionGroup("Additional group:"); |
| 187 | +group.AddOptions(additionalOption); |
| 188 | + |
| 189 | +parser.Parse(new string[] { "--help" }); |
| 190 | +``` |
| 191 | + |
| 192 | +In particular, this division will be displayed in the help output. It will be like the following: |
| 193 | + |
| 194 | +``` |
| 195 | +Usage: [--angle ANGLE] [--verbose] [--help] [--version] |
| 196 | +
|
| 197 | +Default group: |
| 198 | + --angle ANGLE rotation angle |
| 199 | + --help, -h show command-line help |
| 200 | + --version show version information |
| 201 | +
|
| 202 | +Additional group: |
| 203 | + --verbose be verbose |
| 204 | +``` |
0 commit comments