简体   繁体   English

在Java Main方法的命令行参数中指定“用法”的正确方法是什么?

[英]What is the Proper Way to Specify “Usage” in Command Line Arguments for Java Main method?

I have a main method that takes an unlimited number of arguments. 我有一个主方法,可以接受无数个参数。

It is in the form of: 它的形式为:

    if (args.length == 0) {
        // This is what I would like to explain better
        System.out.println("Usage: selectField1 [...] whereConditionField:Value [...]");
        System.exit(0);
    }
    List<String> selectList = new ArrayList<String>();
    BooleanQuery booleanQuery = new BooleanQuery();
    for (String arg : args) {

        if (arg.contains(":")) {
            // Argument is a WHERE condition
            String[] keyValue = arg.split(":");
            Term term = new Term(keyValue[0], keyValue[1]);
            booleanQuery.add(new TermQuery(term), BooleanClause.Occur.MUST);
        } else {
            // Argument is a SELECT field
            selectList.add(arg);
        }
    }

What is the proper way to explain this in the Usage output? 在用法输出中解释此问题的正确方法是什么?

This is a subjective question, due to the fact that what is easy to understand for some persons can be difficult for others. 这是一个主观的问题,因为对于某些人来说容易理解的内容对于其他人来说可能很难。 You can use something like the format of some of the linux command tools, for example: 您可以使用某些linux命令工具的格式,例如:

man cat 

Outputs: 输出:

NAME
    cat - concatenate files and print on the standard output

SYNOPSIS
    cat [OPTION]... [FILE]...

The [OPTION]... notation is widely used to be indicate several (optional) arguments. [OPTION]...符号广泛用于表示几个(可选)参数。 Also, take the example of the command line utillty zip , when you run it without options will output: 另外,以命令行utillty zip为例,在运行时不带选项将输出:

Copyright (c) 1990-2008 Info-ZIP - Type 'zip "-L"' for software license.
Zip 3.0 (July 5th 2008). Usage:
zip [-options] [-b path] [-t mmddyyyy] [-n suffixes] [zipfile list] [-xi list]
...

Explicitly saying that you expect a zipfile list is easy for users to understand. 明确地说,您希望zipfile list易于用户理解。

In your case, something like: 就您而言,类似:

Usage: [SELECT_FIELD]... [WHERE_CONDITION_FIELD:VALUE]...

Or 要么

Usage: [field_list] [condition_pair_list]

Or any other combination... One thing for sure, if you want your users to understand the proper usage of your command line program, nothing beats an example, print the format alongside one (or a couple) of usage examples: 或任何其他组合...可以肯定的是,如果您希望用户了解命令行程序的正确用法,那么没有什么比这更好的了,在一个(或几个)用法示例旁边打印格式:

Usage: [SELECT_FIELD]... [WHERE_CONDITION_FIELD:VALUE]...
Example:  
       java programName field1 condition1:Test field2 conditionName:foo

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM