Suggestions for implementation of a command line interface

前端 未结 8 1974
野的像风
野的像风 2020-12-23 03:46

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 co

相关标签:
8条回答
  • 2020-12-23 04:25

    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.

    0 讨论(0)
  • 2020-12-23 04:29

    A good and helpful reference:

    https://commandline.codeplex.com/

    Library available via NuGet:

    1. Latest stable: Install-Package CommandLineParser.
    2. Latest release: Install-Package CommandLineParser -pre.

    One line parsing using default singleton: CommandLine.Parser.Default.ParseArguments(...).
    One line help screen generator: HelpText.AutoBuild(...).
    Map command line arguments to IList<string>, arrays, enum or standard scalar types.
    Plug-In friendly architecture as explained here.
    Define verb commands as git commit -a.
    Create parser instance using lambda expressions.

    QuickStart: https://commandline.codeplex.com/wikipage?title=Quickstart&referringTitle=Documentation

    // Define a class to receive parsed values
    class Options {
      [Option('r', "read", Required = true,
        HelpText = "Input file to be processed.")]
      public string InputFile { get; set; }
    
      [Option('v', "verbose", DefaultValue = true,
        HelpText = "Prints all messages to standard output.")]
      public bool Verbose { get; set; }
    
      [ParserState]
      public IParserState LastParserState { get; set; }
    
      [HelpOption]
      public string GetUsage() {
        return HelpText.AutoBuild(this,
          (HelpText current) => HelpText.DefaultParsingErrorsHandler(this, current));
      }
    }
    
    // Consume them
    static void Main(string[] args) {
      var options = new Options();
      if (CommandLine.Parser.Default.ParseArguments(args, options)) {
        // Values are available here
        if (options.Verbose) Console.WriteLine("Filename: {0}", options.InputFile);
      }
    }
    
    0 讨论(0)
提交回复
热议问题