-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from dotnet-campus/t/lvyi/for-open-source
开源命令行解析库
- Loading branch information
Showing
6 changed files
with
729 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2019 dotnet campus | ||
|
||
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. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
# dotnetCampus.CommandLine | ||
|
||
[English][en]|[简体中文][zh-chs]|[繁體中文][zh-cht] | ||
-|-|- | ||
|
||
[en]: /README.md | ||
[zh-chs]: /docs/zh-chs/README.md | ||
[zh-cht]: /docs/zh-cht/README.md | ||
|
||
dotnetCampus.CommandLine is probably the fastest command line parser in all .NET open-source projects. | ||
|
||
Parsing a classical command line only takes 1091ns, thus 10 ticks. | ||
|
||
## Get Started | ||
|
||
For your program `Main` method, write this code below: | ||
|
||
```csharp | ||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
var commandLine = CommandLine.Parse(args); | ||
var options = commandLine.As<Options>(new OptionsParser()); | ||
|
||
// Then, use your Options instance here. | ||
} | ||
} | ||
``` | ||
|
||
You need to define the `Options` class as followed below: | ||
|
||
```csharp | ||
class Options | ||
{ | ||
[Value(0)] | ||
public string FilePath { get; } | ||
|
||
[Option('s', "Silence")] | ||
public bool IsSilence { get; } | ||
|
||
[Option('m', "Mode")] | ||
public string StartMode { get; } | ||
|
||
[Option("StartupSessions")] | ||
public IReadonlyList<string> StartupSessions { get; } | ||
|
||
public Options( | ||
string filePath, | ||
bool isSilence, | ||
string startMode, | ||
IReadonlyList<string> startupSessions) | ||
{ | ||
FilePath = filePath; | ||
IsSilence = isSilence; | ||
StartMode = startMode; | ||
StartupSessions = startupSessions; | ||
} | ||
} | ||
``` | ||
|
||
Then you can run your program by passing these kind of command line args: | ||
|
||
Windows style: | ||
|
||
```powershell | ||
> demo.exe "C:\Users\lvyi\Desktop\demo.txt" -s -Mode Edit -StartupSessions A B C | ||
``` | ||
|
||
```cmd | ||
> demo.exe "C:\Users\lvyi\Desktop\demo.txt" /s /Mode Edit /StartupSessions A B C | ||
``` | ||
|
||
Linux style: | ||
|
||
```bash | ||
$ demo.exe "C:/Users/lvyi/Desktop/demo.txt" -s --mode Edit --startup-sessions A B C | ||
``` | ||
|
||
Notice that you cannot use different styles in a single command line. | ||
|
||
For `bool`: | ||
|
||
- You can pass `true` / `True` / `false` / `False` to specify a boolean value; | ||
- You can pass nothing but only a switch. | ||
|
||
It means that `-s true`, `-s True`, `-s` are the same. | ||
|
||
For `ValueAttribute` and `OptionAttribute`: | ||
|
||
- You can specify both on a single property. | ||
- If there is a value without option the property got the value, but if another value with the specified option exists, the new value will override the old one. | ||
|
||
```csharp | ||
[Value(0), Option('f', "File")] | ||
public string FilePath { get; } | ||
``` | ||
|
||
## Engage, Contribute and Provide Feedback | ||
|
||
Thank you very much for firing a new issue and providing new pull requests. | ||
|
||
### Issue | ||
|
||
Click here to file a new issue: | ||
|
||
- [New Issue · dotnet-campus/dotnetCampus.CommandLine](https://github.com/dotnet-campus/dotnetCampus.CommandLine/issues/new) | ||
|
||
### Contributing Guide | ||
|
||
Be kindly. | ||
|
||
## License | ||
|
||
dotnetCampus.CommandLine is licensed under the [MIT license](/LICENSE). |
Oops, something went wrong.