Suggestions for implementation of a command line i

2019-01-30 11:42发布

I am redesigning a command line application and am looking for a way to make its use more intuitive. Are there any conventions for the format of parameters passed into a command line application? Or any other method that people have found useful?

17条回答
手持菜刀,她持情操
2楼-- · 2019-01-30 11:46

The conventions that you use for you application would depend on

1) What type of application it is. 2) What operating system you are using.

This is definitely true. I'm not certain about dos-prompt conventions, but on unix-like systems the general conventions are roughly:

1) Formatting is

appName parameters

2) Single character parameters (such as 'x') are passed as -x 3) Multi character parameters (such as 'add-keys') are passed as --add-keys

查看更多
神经病院院长
3楼-- · 2019-01-30 11:48

I've just released an even better command line parser.
https://github.com/gene-l-thomas/coptions
It's on nuget Install-Package coptions

using System;
using System.Collections.Generic;
using coptions;

[ApplicationInfo(Help = "This program does something useful.")]
public class Options
{
    [Flag('s', "silent", Help = "Produce no output.")]
    public bool Silent;

    [Option('n', "name", "NAME", Help = "Name of user.")]
    public string Name
    {
        get { return _name;  }
        set { if (String.IsNullOrWhiteSpace(value))
                throw new InvalidOptionValueException("Name must not be blank");
              _name = value;
        }
    }
    private string _name;

    [Option("size", Help = "Size to output.")]
    public int Size = 3;

    [Option('i', "ignore", "FILENAME", Help = "Files to ignore.")]
    public List<string> Ignore;

    [Flag('v', "verbose", Help = "Increase the amount of output.")]
    public int Verbose = 1;

    [Value("OUT", Help = "Output file.")]
    public string OutputFile;

    [Value("INPUT", Help = "Input files.")]
    public List<string> InputFiles;
}

namespace coptions.ReadmeExample
{
    class Program
    {
        static int Main(string[] args)
        {
            try
            {
                Options opt = CliParser.Parse<Options>(args);

                Console.WriteLine(opt.Silent);
                Console.WriteLine(opt.OutputFile);
                return 0;
            }
            catch (CliParserExit)
            {
                // --help
                return 0;

            } catch (Exception e)
            {
                // unknown options etc...
                Console.Error.WriteLine("Fatal Error: " + e.Message);
                return 1;
            }
        }
    }
}

Supports automatic --help generation, verbs, e.g. commmand.exe
Enjoy.

查看更多
萌系小妹纸
4楼-- · 2019-01-30 11:49

Complementing @vonc's answer, don't accept ambiguous abbreviations. Eg:

  myCli.exe describe someThing
  myCli.exe destroy someThing
  myCli.exe des someThing ???

In fact, in that case, I probably wouldn't accept an abbreviation for "destroy"...

查看更多
The star\"
5楼-- · 2019-01-30 11:51

I always add a /? parameter to get help and I always try to have a default (i.e. most common scenario) implementation.

Otherwise I tend to use the "/x" for switches and "/x:value" for switches that require values to be passed. Makes it pretty easy to parse the parameters using regular expressions.

查看更多
走好不送
6楼-- · 2019-01-30 11:51

I've created a .Net C# library that includes a command-line parser. You just need to create a class that inherits from the CmdLineObject class, call Initialize, and it will automatically populate the properties. It can handle conversions to different types (uses an advanced conversion library also included in the project), arrays, command-line aliases, click-once arguments, etc. It even automatically creates command-line help (/?).

If you are interested, the URL to the project is http://bizark.codeplex.com. It is currently only available as source code.

查看更多
做自己的国王
7楼-- · 2019-01-30 11:52

I see a lot of Windows command line specifics, but if your program is intended for Linux, I find the GNU command line standard to be the most intuitive. Basically, it uses double hyphens for the long form of a command (e.g., --help) and a single hyphen for the short version (e.g., -h). You can also "stack" the short versions together (e.g., tar -zxvf filename) and mix 'n match long and short to your heart's content.

The GNU site also lists standard option names.

The getopt library greatly simplifies parsing these commands. If C's not your bag, Python has a similar library, as does Perl.

查看更多
登录 后发表回答