0

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?

4

1 回答 1

1

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:

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. Also, take the example of the command line utillty zip, when you run it without options will output:

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.

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
于 2013-10-03T20:00:59.737 回答