This C# client library that provides light weight functionality to help process command line options.
Please ⭐ star this project!
git clone https://github.com/jamesjohnmcguire/CommandLineCommands
PM> Install-Package DigitalZenWorks.CommandLine.Commands
Usage is intended to be simple and direct - Process command line arguments and get going with your business.
NEW!
Define a list of valid commands in JSON file, then load the file into the library. Here is an example of the JSON command definitions file:
[
{
"command": "help",
"description": "Show this information"
},
{
"command": "dbx-to-pst",
"description": "Migrate dbx files to pst file",
"options":
[
{
"shortName": "e",
"longName": "encoding",
"requiresParameter": true
}
],
"parameters":
[
"dbx files path",
"PST file path"
]
},
{
"command": "eml-to-pst",
"description": "Migrate eml files to pst file",
"options":
[
{
"shortName": "a",
"longName": "adjust",
"requiresParameter": false
}
],
"parameters":
[
"eml files path",
"PST file path"
]
},
{
"command": "list-folders",
"description": "List all sub folders of a given store or folder",
"options":
[
{
"shortName": "r",
"longName": "recurse",
"requiresParameter": false
}
],
"parameters":
[
"PST file path",
"folder path"
]
},
{
"command": "remove-duplicates",
"description": "Remove duplicate messages",
"options":
[
{
"shortName": "n",
"longName": "dryrun",
"requiresParameter": false
},
{
"shortName": "s",
"longName": "flush",
"requiresParameter": false
}
],
"parameters":
[
"PST file path"
]
}
]
Note: This example taken from: https://github.com/jamesjohnmcguire/DigitalZenWorks.Email.ToolKit
Otherwise, to pragmatically create a list of commands that the application will support, here is an example:
IList<Command> commands = new List<Command>();
Command help = new ("help");
help.Description = "Show this information";
commands.Add(help);
CommandOption encoding = new ("e", "encoding", true);
IList<CommandOption> options = new List<CommandOption>();
options.Add(encoding);
Command someCommand = new (
"some-command", options, 1, "A Do Something Command");
commands.Add(someCommand);
The third parameter is the minimum amount of required parameters, such as data file path. The other parameters should be self-explanatory.
Then instantiate a CommandLineInstance object:
CommandLineInstance commandLine = new (commands, arguments);
If all goes well and the supplied arguments pass validation, the active command object will be available, and you can get on your way:
if (commandLine.ValidArguments == true)
{
Command command = commandLine.Command;
switch (command.Name)
{
case "some-command":
DoSomething(command);
break;
}
}
Getting the associated additional parameters and options, can be done as follows:
string dataFilePath = command.Parameters[0];
bool dryRun = command.DoesOptionExist("n", "dryrun");
If you have a suggestion that would make this better, please fork the repo and create a pull request. You can also simply open an issue with the tag "enhancement".
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
Please match the current coding style. Most notably:
- One operation per line
- Use complete English words in variable and method names
- Attempt to declare variable and method names in a self-documenting manner
Distributed under the MIT License. See LICENSE
for more information.
James John McGuire - @jamesmc - [email protected]
Project Link: https://github.com/jamesjohnmcguire/CommandLineCommands