Giter Club home page Giter Club logo

jsoncommandline's Introduction

JsonCommandLine

Provides a simple and easy API to pass JSON command line arguments. and yes, it can parse arguments which are generated like so too!

JsonCommandLine library works little bit differently compared to other CommandLine parsers. Every argument must have a base Command keyword. It can have multiple parameters. Command keyword is used to identify the argument and the parameters are used to pass values with reference to command keyword. Parameters are stored in Dictionary<string,string>() while BaseArgument is a single string

Sample usage

static void Main(string[] args) {
			// parsing args
			using (CommandLineParser parser = new CommandLineParser(Environment.CommandLine)) {
				// check if this args contain values are generated by JsonCommandLine
				// if its not, we can't parse it
				if (!parser.IsJsonType) {
					return;
				}

				// parses the arguments
				var arguments = parser.Parse();

				if (arguments.ArgumentsExist) {
					foreach(var arg in arguments.ArgumentCollection) {
						// the base command
						Console.WriteLine(arg.BaseCommand);

						foreach(var param in arg.Parameters) {
							// parameters of the above base command
							Console.WriteLine($"{param.Key} -> {param.Value}");
						}
					}
				}
			}

			// generating args
			// initiate an argument with base command
			CommandLineArgument argument = new CommandLineArgument("update");

			// add parameters to the base command
			argument.TryAddParameter("enableTrace", "false");
			argument.TryAddParameter("generateBackup", "true");

			// generate builder to chain multiple arguments into single instance
			argument.GenerateArgumentBuilder()
				// add another argument
				.Add(new CommandLineArgument("restart", new Dictionary<string, string>() {
					{"path", "foo/bar/word.exe" }
				}))
				// maybe add another ?
				.Add(new CommandLineArgument("shutdownTimed", new Dictionary<string, string>() {
					{"delaySeconds", "10" }
				}))
				// either build as Arguments object which contains all chained command line arguments
				// .BuildAsArgument();
				// or
				// as resultant command line argument string which will be passed to the next process as args.
				// .Build();
				.Build()

				// we call AsArgument() on Arguments object to turn the chained arguments into resultant args string
				// above BuildAsArgument() function does the same, just that it directly converts it to string args.
				.AsArgument();
		}
  • Dependencies
    • Newtonsoft.Json (>= 12.0.3)

jsoncommandline's People

Contributors

arunprakashg avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.