-
Notifications
You must be signed in to change notification settings - Fork 11
/
Program.cs
executable file
·183 lines (152 loc) · 8.37 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
using System;
using System.Reflection;
using System.Collections.Generic;
using Microsoft.Extensions.CommandLineUtils;
namespace ConsoleArgs
{
class Program
{
static void Main(string[] args)
{
// Instantiate the command line app
var app = new CommandLineApplication();
// This should be the name of the executable itself.
// the help text line "Usage: ConsoleArgs" uses this
app.Name = "ConsoleArgs";
app.Description = ".NET Core console app with argument parsing.";
app.ExtendedHelpText = "This is a sample console app to demostrate the usage of Microsoft.Extensions.CommandLineUtils."
+ Environment.NewLine + "Depending on your OS, you may need to execute the application as ConsoleArgs.exe or 'dotnet ConsoleArgs.dll'";
// Set the arguments to display the description and help text
app.HelpOption("-?|-h|--help");
// This is a helper/shortcut method to display version info - it is creating a regular Option, with some defaults.
// The default help text is "Show version Information"
app.VersionOption("-v|--version", () => {
return string.Format("Version {0}", Assembly.GetEntryAssembly().GetCustomAttribute<AssemblyInformationalVersionAttribute>().InformationalVersion);
});
// The first argument is the option template.
// It starts with a pipe-delimited list of option flags/names to use
// Optionally, It is then followed by a space and a short description of the value to specify.
// e.g. here we could also just use "-o|--option"
var basicOption = app.Option("-o|--option <optionvalue>",
"Some option value",
CommandOptionType.SingleValue);
// Arguments are basic arguments, that are parsed in the order they are given
// e.g ConsoleArgs "first value" "second value"
// This is OK for really simple tasks, but generally you're better off using Options
// since they avoid confusion
var argOne = app.Argument("argOne", "App argument one");
var argTwo = app.Argument("argTwo", "App argument two");
// When no commands are specified, this block will execute.
// This is the main "command"
app.OnExecute(() =>
{
Console.WriteLine("Argument value one: {0}", argOne.Value ?? "null");
Console.WriteLine("Argument value two: {0}", argTwo.Value ?? "null");
//You can also use the Arguments collection to iterate through the supplied arguments
foreach (CommandArgument arg in app.Arguments)
{
Console.WriteLine("Arguments collection value: {0}", arg.Value ?? "null");
}
// Use the HasValue() method to check if the option was specified
if (basicOption.HasValue())
{
Console.WriteLine("Option was selected, value: {0}", basicOption.Value());
}
else
{
Console.WriteLine("No options specified.");
// ShowHint() will display: "Specify --help for a list of available options and commands."
app.ShowHint();
}
return 0;
});
// This is a command with no arguments - it just does default action.
app.Command("simple-command", (command) =>
{
//description and help text of the command.
command.Description = "This is the description for simple-command.";
command.ExtendedHelpText = "This is the extended help text for simple-command.";
command.HelpOption("-?|-h|--help");
command.OnExecute(() =>
{
Console.WriteLine("simple-command is executing");
//Do the command's work here, or via another object/method
Console.WriteLine("simple-command has finished.");
return 0; //return 0 on a successful execution
});
}
);
app.Command("complex-command", (command) =>
{
// This is a command that has it's own options.
command.ExtendedHelpText = "This is the extended help text for complex-command.";
command.Description = "This is the description for complex-command.";
command.HelpOption("-?|-h|--help");
// There are 3 possible option types:
// NoValue
// SingleValue
// MultipleValue
// MultipleValue options can be supplied as one or multiple arguments
// e.g. -m valueOne -m valueTwo -m valueThree
var multipleValueOption = command.Option("-m|--multiple-option <value>",
"A multiple-value option that can be specified multiple times",
CommandOptionType.MultipleValue);
// SingleValue: A basic Option with a single value
// e.g. -s sampleValue
var singleValueOption = command.Option("-s|--single-option <value>",
"A basic single-value option",
CommandOptionType.SingleValue);
// NoValue are basically booleans: true if supplied, false otherwise
var booleanOption = command.Option("-b|--boolean-option",
"A true-false, no value option",
CommandOptionType.NoValue);
command.OnExecute(() =>
{
Console.WriteLine("complex-command is executing");
// Do the command's work here, or via another object/method
// Grab the values of the various options. when not specified, they will be null.
// The NoValue type has no Value property, just the HasValue() method.
bool booleanOptionValue = booleanOption.HasValue();
// MultipleValue returns a List<string>
List<string> multipleOptionValues = multipleValueOption.Values;
// SingleValue returns a single string
string singleOptionValue = singleValueOption.Value();
// Check if the various options have values and display them.
// Here we're checking HasValue() to see if there is a value before displaying the output.
// Alternatively, you could just handle nulls from the Value properties
if(booleanOption.HasValue())
{
Console.WriteLine("booleanOption option: {0}", booleanOptionValue.ToString());
}
if(multipleValueOption.HasValue())
{
Console.WriteLine("multipleValueOption option(s): {0}", string.Join(",", multipleOptionValues));
}
if(singleValueOption.HasValue())
{
Console.WriteLine("singleValueOption option: {0}", singleOptionValue ?? "null");
}
Console.WriteLine("complex-command has finished.");
return 0; // return 0 on a successful execution
});
});
try
{
// This begins the actual execution of the application
Console.WriteLine("ConsoleArgs app executing...");
app.Execute(args);
}
catch (CommandParsingException ex)
{
// You'll always want to catch this exception, otherwise it will generate a messy and confusing error for the end user.
// the message will usually be something like:
// "Unrecognized command or argument '<invalid-command>'"
Console.WriteLine(ex.Message);
}
catch (Exception ex)
{
Console.WriteLine("Unable to execute application: {0}", ex.Message);
}
}
}
}