Skip to content

Commit 1b289d4

Browse files
authored
Merge pull request #1 from jpdillingham/dev
Implemented operand parsing
2 parents 2a147d6 + 70efe4d commit 1b289d4

File tree

7 files changed

+485
-159
lines changed

7 files changed

+485
-159
lines changed

.travis.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
os:
22
- linux
3-
- osx
43
language: csharp
54
solution: Utility.CommandLine.Arguments.sln
65
install:

Examples/Program.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ internal class Program
2020
[Argument('i', "integer")]
2121
private static int Int { get; set; }
2222

23+
[Operands]
24+
private static string[] Operands { get; set; }
25+
2326
[Argument('s', "string")]
2427
private static string String { get; set; }
2528

@@ -71,7 +74,7 @@ private static void Print(string commandLine)
7174
Console.WriteLine("\r\nArgument\tValue");
7275
Console.WriteLine("-------\t\t-------");
7376

74-
Dictionary<string, string> argumentDictionary = Arguments.Parse(commandLine);
77+
Dictionary<string, string> argumentDictionary = Arguments.Parse(commandLine).ArgumentDictionary;
7578

7679
foreach (string key in argumentDictionary.Keys)
7780
{
@@ -85,6 +88,13 @@ private static void Print(string commandLine)
8588
Console.WriteLine("Bool\t\t" + Bool);
8689
Console.WriteLine("Int\t\t" + Int);
8790
Console.WriteLine("Double\t\t" + Double);
91+
92+
Console.WriteLine("\r\nOperands\n-------");
93+
94+
for (int i = 0; i < Operands.Length; i++)
95+
{
96+
Console.WriteLine(i + ".\t" + Operands[i]);
97+
}
8898
}
8999

90100
/// <summary>

README.md

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ Install from the NuGet gallery GUI or with the Package Manager Console using the
1818

1919
```Install-Package Utility.CommandLine.Arguments```
2020

21+
The code is also designed to be incorporated into your project as a single source file (Arguments.cs).
22+
2123
## Quick Start
2224

2325
Create private static properties in the class containing your ```Main()``` and mark them with the ```Argument``` attribute, assigning short and long names. Invoke
@@ -41,6 +43,9 @@ internal class Program
4143
[Argument('s', "string")]
4244
private static string String { get; set; }
4345

46+
[Operands]
47+
private static string[] Operands { get; set; }
48+
4449
private static void Main(string[] args)
4550
{
4651
Arguments.Populate();
@@ -49,6 +54,11 @@ internal class Program
4954
Console.WriteLine("Bool: " + Bool);
5055
Console.WriteLine("Int: " + Int);
5156
Console.WriteLine("Double: " + Double);
57+
58+
foreach (string operand in Operands)
59+
{
60+
Console.WriteLine("\r\n Operand:" + operand);
61+
}
5262
}
5363
}
5464
```
@@ -61,7 +71,7 @@ located [here](http://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap1
6171

6272
Each argument is treated as a key-value pair, regardless of whether a value is present. The general format is as follows:
6373

64-
```<-|--|/>argument-name<=|:| >["|']value['|"]```
74+
```<-|--|/>argument-name<=|:| >["|']value['|"] [operand] ... [operand]```
6575

6676
The key-value pair may begin with a single dash, a pair of dashes (double dash), or a forward slash. Single and double dashes indicate the use of short
6777
or long names, respectively, which are covered below. The forward slash may represent either, but does not allow for the grouping of parameterless
@@ -73,6 +83,9 @@ The value delimiter may be an equals sign, a colon, or a space.
7383

7484
Values may be any alphanumeric sequence, however if a value contains a space it must be enclosed in either single or double quotes.
7585

86+
Any word, or phrase enclosed in single or double quotes, will be parsed as an operand. The official specification requires operands to appear last, however this library will
87+
parse them in any position.
88+
7689
### Short Names
7790

7891
Short names consist of a single character, and arguments without parameters may be grouped. A grouping may be terminated with a single argument containing
@@ -143,6 +156,23 @@ c | foo
143156
hello | world
144157
new | slashes are ok too
145158

159+
### Operands
160+
161+
Any text in the string that doesn't match the argument-value format is considered an operand.
162+
163+
#### Example
164+
165+
```-a foo bar "hello world" -b```
166+
167+
Key | Value
168+
--- | ---
169+
a | foo
170+
b |
171+
172+
Operands
173+
1. bar
174+
2. "hello world"
175+
146176
## Parsing
147177

148178
Argument key-value pairs can be parsed from any string using the ```Parse(string)``` method. This method returns a
@@ -175,6 +205,9 @@ whether the argument was encountered in the command line arguments. All other v
175205
The ```Populate()``` method uses reflection to populate private static properties in the target Type with the argument
176206
values matching properties marked with the ```Argument``` attribute.
177207

208+
The list of operands is placed into a single property marked with the ```Operands``` attribute. This property must be
209+
of type ```string[]``` or ```List<string>```.
210+
178211
### Creating Target Properties
179212

180213
Use the ```Argument``` attribute to designate properties to be populated. The constructor of ```Argument``` accepts a ```char``` and a
@@ -185,6 +218,9 @@ Note that the name of the property doesn't matter; only the attribute values are
185218
The Type of the property does matter; the code attempts to convert argument values from string to the specified Type, and if the conversion fails
186219
an ```ArgumentException``` is thrown.
187220

221+
The ```Operands``` property accepts no parameters. If the property type is not ```string[]``` or ```List<string>```, an
222+
```InvalidCastException``` will be thrown.
223+
188224
#### Examples
189225

190226
```c#
@@ -196,6 +232,9 @@ private static integer MyNumber { get; set; }
196232

197233
[Argument('b', "bool")]
198234
private static bool TrueOrFalse { get; set; }
235+
236+
[Operands]
237+
private static List<string> Operands { get; set; }
199238
```
200239

201240
Given the argument string ```-bf "bar" --number=5```, the resulting property values would be as follows:

Utility.CommandLine.Arguments.Tests/ArgumentAttribute.cs

Lines changed: 0 additions & 82 deletions
This file was deleted.

0 commit comments

Comments
 (0)