-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.md
546 lines (401 loc) · 12.1 KB
/
README.md
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
# Commands
[![NuGet](https://img.shields.io/nuget/v/JSSoft.Commands.svg?style=flat)][NuGet]
[![NuGet (prerelease)](https://img.shields.io/nuget/vpre/JSSoft.Commands.svg?style=flat)][NuGet]
[NuGet]: https://www.nuget.org/packages/JSSoft.Commands/
## Summary
Parses a string to set a value to a specified property or invoke a specified method.
Like Terminal or Powershell, it provides a REPL environment to provide a command-based development environment.
## Requirements
```plain
dotnet sdk 8.0.100 or later
c# 12
```
## Clone
```plain
git clone https://github.com/s2quake/commands.git
```
## Build - NET 8.0
```plain
dotnet build
```
## Other Framework Build
```shell
# net7.0
dotnet build -p:TargetFrameworks=net7.0 --framework net7.0
# netcoreapp3.1
dotnet build -p:TargetFrameworks=netcoreapp3.1 --framework netcoreapp3.1
# netstandard2.1
dotnet build -p:TargetFrameworks=netstandard2.1 --framework netstandard2.1
# net481
dotnet build -p:TargetFrameworks=net481 --framework net481
```
## Run Examples
```shell
# Run the property settings example project
dotnet run --project JSSoft.Commands.Parse --framework net8.0 -- --help
# Run the method call example project
dotnet run --project JSSoft.Commands.Invoke --framework net8.0 -- --help
# Run the CommandContext Execution example Project
dotnet run --project JSSoft.Commands.Sets --framework net8.0 -- --help
# Run the CommandContext Execution Example Project in the REPL environment
dotnet run --project JSSoft.Commands.Repl --framework net8.0
# Run the CommandContext Execution Example Project with Avalonia UI
dotnet run --project JSSoft.Commands.AppUI --framework net8.0
```
## Parse
This is the most basic way to parse the command. Provides a function to set a value for a specified property.
```csharp
var settings = new Settings();
var parser = new CommandParser(settings);
parser.Parse(args);
```
> See the JSSoft.Commands.Parse project
## Invoke
As an extension of Parse, it provides the ability to call a specified method by parsing the command.
```csharp
var commands = new Commands();
var invoker = new CommandInvoker(commands);
invoker.Invoke(args);
```
> See the JSSoft.Commands.Invoke project
## CommandContext
It provides various functions to manage and process more commands.
```csharp
var commands = new ICommand[]
{
new LoginCommand(),
new LogoutCommand(),
new ExitCommand()
};
var commandContext = new CommandContext(commands);
commandContext.Execute(args);
or
await commandContext.ExecuteAsync(args);
```
> See the JSSoft.Commands.Sets project
It can be combined with user input such as EditBox, TextBox, InputText to build a console or REPL-like environment.
```csharp
var commands = new ICommand[]
{
new LoginCommand(),
new LogoutCommand(),
new ExitCommand()
};
var commandContext = new CommandContext(commands);
var terminal = new SystemTerminal(commandContext);
await terminal.StartAsync(CancellationToken.None);
```
> See the JSSoft.Commands.Repl project
## Property
### Required argument definition
To define the value required for command syntax, define **CommandPropertyRequired** in the property.
```csharp
[CommandPropertyRequired]
public string Value1 { get; set; } = string.Empty;
[CommandPropertyRequired]
public int Value2 { get; set; }
```
```plain
"value" // error! value for Value2 does not exists.
3 // format error!
"value" 3 // Value1 is "value", Value2 is 3
```
You can set default values like this: If there is no value in the command syntax, it is replaced with the default value.
```csharp
[CommandPropertyRequired]
public string Value1 { get; set; } = string.Empty;
[CommandPropertyRequired(DefaultValue = 1)]
public int Value2 { get; set; }
```
```plain
"value" 2 // Value1 is "value", Value2 is 2
"value" // Value1 is "value", Value2 is 1
```
### Explicit required argument definition
An explicit required argument indicates that the command syntax must have a value, but must include a switch statement, such as --value "2".
```csharp
[CommandPropertyRequired]
public string Value1 { get; set; } = string.Empty;
[CommandPropertyExplicitRequired]
public int Value2 { get; set; }
```
```plain
"value" // error!
"value" 2 // error!
"value" --value2 // error!
"value" --value2 3 // Value1 is "value", Value2 is 3
--value2 3 "value" // Value1 is "value", Value2 is 3
```
In order to use the default values of explicit required arguments, the command syntax must include a switch statement such as --value.
```csharp
[CommandPropertyRequired]
public string Value1 { get; set; } = string.Empty;
[CommandPropertyExplicitRequired(DefaultValue = 1)]
public int Value2 { get; set; }
```
```plain
"value" // error!
"value" 2 // error!
"value" --value2 // Value1 is "value", Value2 is 1
"value" --value2 3 // Value1 is "value", Value2 is 3
--value2 3 "value" // Value1 is "value", Value2 is 3
--value2 "value" // error! "value" is not int
```
### Optional argument definition
The optional argument can be set whether or not to use a value using a switch statement.
```csharp
[CommandProperty]
public string Value { get; set; } = string.Empty;
```
```plain
--value // error
--value text // value is "text"
```
To use the default, the command syntax must include a switch statement such as --value.
```csharp
[CommandProperty(DefaultValue = "1")]
public string Value { get; set; } = string.Empty;
```
```plain
--value // value is "1"
--value text // value is "text"
```
A bool type switch statement that does not use a value should be defined as follows.
```csharp
[CommandPropertySwitch]
public bool Switch { get; set; }
```
### Variable arguments definition
Variable arguments represent the values of the remaining arguments that were not parsed in the command syntax.
The property type of a variable arguments must be an array and must be defined for only one property.
```csharp
[CommandPropertyArray]
public string[] Values { get; set; } = Array.Empty<string>();
```
```plain
-- value1 value2 value3 "value4"
```
## Method
### Method definition
To execute an attribute method through command syntax, you must define a **CommandMethod** in the method as follows.
Each parameter of the method is automatically defined as a required argument.
```csharp
[CommandMethod]
public void Save(string message)
{
}
```
```plain
save "message"
```
If you want to additionally define optional arguments in the method, you can use **CommandMethodProperty** and add the name of the property defined as CommandProperty.
```csharp
[CommandMethod]
[CommandMethodProperty("Value")]
public void Save(string message)
{
}
```
```plain
save "comment"
save "comment" --value text
```
You can use params as below as a variable arguments.
```csharp
[CommandMethod]
public void Save(string message, params string[] args)
{
}
```
```plain
save "comment"
save "comment" -- "1" "text" "string"
```
### Enable or Disable Method
Define the properties by prefixing "Can" to the method name as shown below.
```csharp
public bool Can{MethodName} { get; }
```
example:
```csharp
public bool CanSave => true;
```
## Static properties and methods
Properties and methods defined as static can be included in the object and used.
```csharp
static class GlobalSettings
{
[CommandProperty]
public static string ID { get; set; } = string.Empty;
[CommandProperty]
public static string Password { get; set; }
}
[CommandStaticProperty(typeof(GlobalSettings))]
class Settings
{
}
```
```csharp
static class StaticCommand
{
[CommandMethod]
[CommandMethodProperty(nameof(Value))]
public static void List()
{
}
[CommandProperty]
public static int Value { get; set; }
}
[CommandStaticMethod(typeof(StaticCommand))]
class Commands
{
}
```
## Naming
The property and method names defined as CommandProperty and CommandMethod are changed to [kebab-case (spinal-case, Train-Case, Lisp-case)](https://en.wikipedia.org/wiki/Letter_case).
### Property name example
| Property name | Changed property name |
| --------- | ---------------- |
| Value | --value |
| Message | --message |
| IsLocked | --is-locked |
> When using a name and a short name
```csharp
[CommandProperty("custom-value", 'v')]
public string Value { get; set; } = string.Empty;
```
```plain
--custom-value or -v
```
> When using only short names
```csharp
[CommandProperty('v')]
public string Value { get; set; } = string.Empty;
```
```plain
-v
```
> When using a short name and a default name
```csharp
[CommandProperty('v', AllowName = true)]
public string Value { get; set; } = string.Empty;
```
```plain
-v or --value
```
### Method name example
| Method name | Changed method name |
| ----------- | ------------------ |
| Save | save |
| LockTable | lock-table |
You can also set the method name yourself.
```csharp
[CommandMethod("save")]
public void Save(string message)
{
}
```
## Command
You can define commands in the CommandContext.
```csharp
class ExitCommand : CommandBase
{
[CommandPropertyRequired(DefaultValue = 0)]
public int ExitCode { get; set; }
protected override void OnExecute()
{
Environment.Exit(ExitCode);
}
}
```
```plain
exit
exit 0
```
## SubCommand
You can define commands that have subcommands in CommandContext.
```csharp
class UserCommand : CommandMethodBase
{
[CommandMethod]
[CommandMethodProperty(nameof(Message))]
public void Create(string userID)
{
}
[CommandMethod]
public void Delete(string userID)
{
}
[CommandMethod]
public void List()
{
}
[CommandProperty]
public string Message { get; set; }
}
```
```plain
user create "user1"
user create "user1" --message "new user"
user delete "user1"
user list
```
## SubCommand extension
By implementing a partial class, you can add subcommand to the already implemented command.
```csharp
[PartialCommand]
class UserPartialCommand : CommandMethodBase
{
public UserPartialCommand()
: base("user")
{
}
[CommandMethod]
public void SendMessage(string userID, string message)
{
}
}
```
## SubCommand AsyncMethod
You can use asynchronous methods, as shown in the example below.
The parameters CancellationToken and IProgress<ProgressInfo> are optional, but should always be the last declaration.
For more information, see the ``Choosing the overloads to provide`` topic in the [TAP](https://learn.microsoft.com/en-us/dotnet/standard/asynchronous-programming-patterns/task-based-asynchronous-pattern-tap).
The name of an asynchronous method is used without the suffix Async.
```csharp
class UserCommand : CommandMethodBase
{
[CommandMethod]
public Task Invoke1Async()
{
return Task.CompletedTask;
}
[CommandMethod]
public Task Invoke2Async(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
[CommandMethod]
public Task Invoke3Async(IProgress<ProgressInfo> progress)
{
return Task.CompletedTask;
}
[CommandMethod]
public Task Invoke4Async(CancellationToken cancellationToken, IProgress<ProgressInfo> progress)
{
return Task.CompletedTask;
}
}
```
## License
Released under the MIT License.
Copyright (c) 2024 Jeesu Choi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit
persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.