What is an easy way to use hyphen named inputs in a console application (ie -S "myServerName")?

I want my console application to use inputs with a named name, for example:

myApp -S "myServerName" -D "myDatabaseName"

instead of the usual:

myApp "myServerName" "myDatabaseName"

I think the first is more friendly to developers who want to use my console application in the future. I think I do not know what this type of input is called, otherwise I would find it in google.

+3
source share
3 answers

Everything that is entered after the name of the executable file is called a command line argument.

But regardless of whether you use traits or slashes or any other keyword completely to implement the application.

, .

, . http://www.codeplex.com/cmdline

+3

NDesk.Options. . . :

string server = null;
string database = null;
var p = new OptionSet () {
    { "S",  v => server = v },
    { "D",  v => database = v },
   };
List<string> extra = p.Parse (args); // 'args' comes from Main (string [] args)
...

, :

string server = null;
string database = null;
var p = new OptionSet () {
    { "S", "Server name", v => server = v },
    { "D", "Database name", v => database = v },
    { "h", "Display help", v => { show_help = true; }},
   };
List<string> extra = p.Parse (args); // 'args' comes from Main (string [] args)
if (show_help) {
   Console.WriteLine ("Name of your program and brief description");
   p.WriteOptionDescriptions (Console.Out);
   Environment.Exit (0);
}
...
+1

arg:

    for (int i=0; i < args.Length; i++)
    {
        switch (args[i])
        {
            case "-f":  // string data
                i++;
                if (args.Length <= i) throw new ArgumentException(args[i]);
                _file = args[i];
                break;

            case "-p":  // boolean flag
                _pflag= true;
                break;

            case "-i":  // int argument, allows hex or decimal
                i++;
                if (args.Length <= i) throw new ArgumentException(args[i]);
                if (_intParam != DefaultIntParamValue)
                    throw new ArgumentException(args[i]);
                if (args[i].StartsWith("0x"))
                    _intParam = System.Int32.Parse(args[i].Substring(2), System.Globalization.NumberStyles.AllowHexSpecifier );
                else
                    _intParam = System.Int32.Parse(args[i]);
                break;


            case "-s":  // size, in bytes, K, MB, GB
                i++;
                if (args.Length <= i) throw new Exception(args[i-1]);
                if (args[i].ToUpper().EndsWith("K"))
                    _size = System.Int32.Parse(args[i].Substring(0,args[i].Length-1)) * 1024;
                else if (args[i].ToUpper().EndsWith("KB"))
                    _size = System.Int32.Parse(args[i].Substring(0,args[i].Length-2)) * 1024;
                else if (args[i].ToUpper().EndsWith("M"))
                    _size = System.Int32.Parse(args[i].Substring(0,args[i].Length-1)) * 1024*1024;
                else if (args[i].ToUpper().EndsWith("MB"))
                    _size = System.Int32.Parse(args[i].Substring(0,args[i].Length-2)) * 1024*1024;
                else
                    _size = Int32.Parse(args[i]);
                break;


            case "-?":
                throw new ArgumentException(args[i]);

            default:  // positional argument
                if (_positionalArg != null)
                    throw new ArgumentException(args[i]);

                _positionalArg = args[i];
                break;
        }
    }

I often put this in a runner type constructor.
This logic assumes that arg processing is done inside try..catch, and in catch, Usage mapping. Like this:

try 
{
    MyClass x = new MyClass(args);
    x.DoTheThing(); 
}
catch (Exception exc1)
{
   Usage(); 
}
0
source

Source: https://habr.com/ru/post/1726273/


All Articles