This C# client library that provides light weight functionality to help process command line options.
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.
First, create a list of commands that the application will support, such as:
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 CommandLineArguments object:
CommandLineArguments 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