Suggestions for implementation of a command line interface

前端 未结 8 1973
野的像风
野的像风 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:03

    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.

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

    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"...

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

    If you are using C# try Mono.GetOptions, it's a very powerful and simple-to-use command-line argument parser. It works in Mono environments and with Microsoft .NET Framework.

    EDIT: Here are a few features

    • Each param has 2 CLI representations (1 character and string, e.g. -a or --add)
    • Default values
    • Strongly typed
    • Automagically produces an help screen with instructions
    • Automagically produces a version and copyright screen
    0 讨论(0)
  • 2020-12-23 04:17

    Best thing to do is don't assume anything if you can. When the operator types in your application name for execution and does not have any parameters either hit them with a USAGE block or in the alternative open a Windows Form and allow them to enter everything you need.

    c:\>FOO
    
    FOO
    
    USAGE FOO -{Option}{Value}
    
    -A Do A stuff
    -B Do B stuff
    
    c:\>
    

    Parameter delimiting I place under the heading of a religious topic: hyphens(dashes), double hyphens, slashes, nothing, positional, etc.

    You didn't indicate your platform, but for the next comment I will assume Windows and .net

    You can create a console based application in .net and allow it to interact with the Desktop using Forms just by choosing the console based project then adding the Windows.Forms, System.Drawing, etc DLLs.

    We do this all the time. This assures that no one takes a turn down a dark alley.

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

    Here's a CodeProject article that might help you out...

    C#/.NET Command Line Arguments Parser

    IF VB is your flavor, here's a separate article (with a bit more guidance related content) to check out...

    Parse and Validate Command Line Parameters with VB.NET

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

    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.

    0 讨论(0)
提交回复
热议问题