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:54

I developed this framework, maybe it helps:

The SysCommand is a powerful cross-platform framework, to develop Console Applications in .NET. Is simple, type-safe, and with great influences of the MVC pattern.

https://github.com/juniorgasparotto/SysCommand

namespace Example.Initialization.Simple
{
    using SysCommand.ConsoleApp;

    public class Program
    {
        public static int Main(string[] args)
        {
            return App.RunApplication();
        }
    }

    // Classes inheriting from `Command` will be automatically found by the system
    // and its public properties and methods will be available for use.
    public class MyCommand : Command
    {
        public void Main(string arg1, int? arg2 = null)
        {
            if (arg1 != null)
                this.App.Console.Write(string.Format("Main arg1='{0}'", arg1));
            if (arg2 != null)
                this.App.Console.Write(string.Format("Main arg2='{0}'", arg2));
        }

        public void MyAction(bool a)
        {
            this.App.Console.Write(string.Format("MyAction a='{0}'", a));
        }
    }
}

Tests:

// auto-generate help
$ my-app.exe help

// method "Main" typed
$ my-app.exe --arg1 value --arg2 1000

// or without "--arg2"
$ my-app.exe --arg1 value

// actions support
$ my-app.exe my-action -a
查看更多
淡お忘
3楼-- · 2019-01-30 11:55

One thing I like about certain CLI is the usage of shortcuts.
I.e, all the following lines are doing the same thing

myCli.exe describe someThing
myCli.exe descr someThing
myCli.exe desc someThing

That way, the user may not have to type the all command every time.

查看更多
男人必须洒脱
4楼-- · 2019-01-30 11:56

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. Linux? Windows? They both have different conventions.

What I would suggest is look at other command line interfaces for other commands on your system, paying special attention to the parameters passed. Having incorrect parameters should give the user solution directed error message. An easy to find help screen can aid in usability as well.

Without know what exactly your application will do, it's hard to give specific examples.

查看更多
5楼-- · 2019-01-30 11:57

-operation [parameters] -command [your command] -anotherthings [otherparams]....

For example,

YourApp.exe -file %YourProject.prj% -Secure true
查看更多
做自己的国王
6楼-- · 2019-01-30 11:58

Command line conventions vary from OS to OS, but the convention that's probably gotten both the most use, and the most public scrutiny is the one supported by the GNU getopt package. See http://www.gnu.org/software/libc/manual/html_node/Using-Getopt.html for more info.

It allows you to mix single letter commands, such as -nr, with longer, self-documenting options, such as --numeric --reverse. Be nice, and implement a --help (-?) option and then your users will be able to figure out all they need to know.

查看更多
Ridiculous、
7楼-- · 2019-01-30 12:02

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);
  }
}
查看更多
登录 后发表回答