Skip to content

Commit 990c3fa

Browse files
authored
Merge pull request #51 from AlexGhiondea/AlexGhiondea-patch-3
Add new features to readme
2 parents 12dfa91 + be80784 commit 990c3fa

1 file changed

Lines changed: 76 additions & 1 deletion

File tree

README.md

Lines changed: 76 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ There are 2 kinds of arguments:
8585

8686
## Advanced scenarios
8787

88+
### Argument grouping
8889
There are cases when you want to have different sets of arguments depending on one of the arguments.
8990

9091
The argument that will be used as the differentiator will be marked with the `ActionArgument` attribute. That argument must be specified.
@@ -95,7 +96,6 @@ A single property in a class can take part in many argument groups.
9596

9697
If a property will be common to all groups, you can use the `CommonArgument' attribute
9798

98-
9999
```csharp
100100
internal class CommandLineOptions
101101
{
@@ -135,3 +135,78 @@ internal class CommandLineOptions
135135

136136
}
137137
```
138+
139+
### Argument position override
140+
141+
Sometimes you want to have a common parameter shared across multiple commands, but you want the position of it to be different in those two groups.
142+
143+
To do that, you use the constructor for the `ArgumentGroup` attribute that takes as the second argument the override position for that argument group.
144+
145+
In the example below, the common parameter `repos` is defined as required in position `1`.
146+
For the group `Create` where no override position is specified, the parameter will be required at position `1`.
147+
For the group `List` where the override position is specified, the parameter will be required at position `0`.
148+
149+
```csharp
150+
class OverridePositionGroup
151+
{
152+
[ActionArgument]
153+
public Action Action { get; set; }
154+
155+
[ArgumentGroup(nameof(Action.Create))]
156+
[RequiredArgument(0, "milestoneInputFile", "The file containing the list of milestones to create.")]
157+
public string MilestoneFile { get; set; }
158+
159+
[ArgumentGroup(nameof(Action.List), 0)]
160+
[ArgumentGroup(nameof(Action.Create))]
161+
[RequiredArgument(1, "repos", "The list of repositories where to add the milestones to. The format is: owner\\repoName.", true)]
162+
public List<string> Repositories { get; set; }
163+
}
164+
```
165+
166+
### Background color
167+
168+
The parser will automatically detect the color to use when displaying help in a command prompt. There are a set of colors that are predefined depending on the console color.
169+
170+
You can specify different colors to be used by implementing the `IColor` interface and configuring the parser:
171+
172+
```csharp
173+
public class CustomColors : IColors
174+
{
175+
public ConsoleColor ErrorColor => ConsoleColor.Red;
176+
public ConsoleColor AssemblyNameColor => ConsoleColor.DarkGray;
177+
public ConsoleColor ArgumentGroupColor => ConsoleColor.DarkGreen;
178+
public ConsoleColor RequiredArgumentColor => ConsoleColor.Magenta;
179+
public ConsoleColor ArgumentValueColor => ConsoleColor.DarkGreen;
180+
public ConsoleColor OptionalArgumentColor => ConsoleColor.DarkBlue;
181+
}
182+
183+
// Configure the parser to use the new colors
184+
Parser.Configuration.DisplayColors.Set(new CustomColors());
185+
```
186+
187+
### Environment variable parsing
188+
189+
By default, the parser will try and infer optional parameters from the environment variables if they are not specified in the command line.
190+
191+
The order of precedence is going to be (from most specific to least specific):
192+
1. Values passed in via the command line
193+
2. Values specified in the environment
194+
3. Default value specified in the argument type
195+
196+
By default, the Parser will look for environment variables with this name:
197+
198+
```
199+
CommandLine_<name>
200+
```
201+
202+
The `<name>` represents the optional parameter name as defined in the type declaration.
203+
204+
You can change the prefix for the Parser:
205+
```csharp
206+
Parser.Configuration.EnvironmentVariablePrefix = "myPrefix";
207+
```
208+
209+
You can also disable the feature completely:
210+
```csharp
211+
Parser.Configuration.UseEnvironmentVariables = false;
212+
```

0 commit comments

Comments
 (0)