args4j

Related tags

CLI args4j
Overview

args4j

args4j is a small Java class library that makes it easy to parse command line options/arguments in your CUI application. See more info at [http://args4j.kohsuke.org/]

Why should I use it?

See the quick intro

  • It makes command line parsing very easy by using annotations
  • Generate usage text very easily
  • Generate HTML/XML documentation listing all options
  • Full localization support
  • Designed to parse javac like options, as opposed to GNU-style (where ls -lR is considered to have two options l and R).
  • Licensed under the MIT license.

How can I use it?

  1. Check the sample. This is how your code will look like.
  2. Download the distribution or include the library from the Maven Repository.
  3. Write your code.

More Resources

  1. A small tutorial for the use of the Starter and Args4J
  2. javadoc
  3. How to generate a documentation for your CLI
  4. Extend args4j to handle other Java types
  5. [Kohsuke's Blog: Parsing command line options in JDK 5.0 style] (http://weblogs.java.net/blog/kohsuke/archive/2005/05/parsing_command.html)
  6. A comparison between Commons CLI and Args4j in French
Comments
  • ability not to sort options

    ability not to sort options

    new constructor for CmdLineParser to optionally not sort options. This is because the order of the options might be purposeful, for example grouping them logically.

    opened by javaerb 11
  • forbids and depends modifier does not work

    forbids and depends modifier does not work

    @Option(name="-u",required=true,aliases="--username",usage="Username for login")
    private String username;
    @Option(name="-g",forbids={"-u"},aliases="--generate",usage="generate random key")
    private boolean generate;
    

    When i use this option i get this two errors:

    Options.java:24: error: cannot find symbol
    @Option(name="-g",forbids={"-u"},aliases="--generate",usage="generate random key")
                      ^
    symbol:   method forbids()
    location: @interface Option
    Options.java:24: error: illegal initializer for <none>
    @Option(name="-g",forbids={"-u"},aliases="--generate",usage="generate random key")
                              ^
    2 errors
    
    opened by erolg 6
  • #41 Possibility to exclude Options if other option is present

    #41 Possibility to exclude Options if other option is present

    Hi,

    This is Mingtao. It's my first attempt in open source ... Seems unit-test and integration-test both passed.

    Target on fixing the above scenario. Specifically for -h, which let -h possibly forbids other options.


    I really have trouble to remove several files that I don't want to commit ... .classpath .gitignore bin/META-INF/services/com.sun.mirror.apt.AnnotationProcessorFactory and those *.class files.

    opened by MagIciaNGTAO 6
  • OptionHandler for MAC addresses added.

    OptionHandler for MAC addresses added.

    For a project of mine I needed to parse MAC addresses. Mostly, MAC addresses are represented by a byte array of length 6 in Java. The string representation of a MAC address can be of different forms, e.g.

    XXXXXXXXXXXX XX XX XX XX XX XX XX-XX-XX-XX-XX-XX XX:XX:XX:XX:XX:XX

    where X is a hexadecimal digit. I used an abstract delimiter (name it #) with the only constraint that # does not contain any hexadecimal digit. As an alternative, one can enter a string with 12 letters only containing hexadecimal digits.

    You may want to merge my code to Your project. If You need more help (e.g. because I forgot something or made something wrong) please ask.

    opened by teiesti 6
  • ability to use key=value in usage for options

    ability to use key=value in usage for options

    Right now usage displays options in a form like "--key value". Want the option to show "--key=value". This is determined by a setting in CmdLineParser, "useEqualsforOptionUsage"

    opened by javaerb 5
  • Code cleanups 2

    Code cleanups 2

    Some minor code cleanups

    • compression of null pointer checks in JDK 1.6 compatible way
    • some small JavaDoc for a newly added interface
    • smaller cleanups
    opened by sfuhrm 5
  • Does multiValued require JavaBean implementation?

    Does multiValued require JavaBean implementation?

    I commonly use Args4J annotations on my "set argument" methods. For example:

    @Option(name = "-numdays", usage = "number of days up to and including lastdate to process", required = false)
    public void setNumDays(int numDays) {
        _numDays = numDays;
    }
    

    I'd like to use the multiValued=true qualifier in the same manner, but although the following compiles...

    @Option(name = "-importdir", usage = "directory containing crawl info to import", required = true, multiValued = true)
    public void setImportDirs(List<String> importDirs) {
        _importDirs = importDirs;
    }
    

    ...I get an error at runtime when I try to specify this argument multiple times (e.g., "-importdir dir1 -importdir dir2"):

    org.kohsuke.args4j.IllegalAnnotationError: No OptionHandler is registered to handle interface java.util.List

    I note that MultivaluedTest applies the annotation to the instance variable directly. Is this the only way to make use of the multiValued qualifier, or have I made some other egregious error in my implementation?

    opened by Schmed 5
  • [Fixes Issue #12] Changed MapSetter parser

    [Fixes Issue #12] Changed MapSetter parser

    The MapSetter class now correctly deals with multiple '=' signs being present in the value part. This changes the behaviour as follows:

    Input: "foo=bar=baz" Old (key, value): ("foo", "bar") New (key, value): ("foo", "bar=baz")

    Converted mixed indentation to only-tabs. Added copyright statement and MIT licence text.

    Fixes: https://github.com/kohsuke/args4j/issues/12 Signed-off-by: Martin Schroeder [email protected]

    opened by HedAurabesh 5
  • print seperating colon in usage only for the first line

    print seperating colon in usage only for the first line

    ... for better readability.

    Fixes #54

    New Output:

    "xy" is not a valid value for "-e"
     -e (--environment) training, productio : application environment (default:
     n, testing, development, custom          development)
    
    opened by thomas-mc-work 4
  • added support for args in format -argname=argvalue

    added support for args in format -argname=argvalue

    Hi Kohsuke,

    Args4j should definitely have the following feature (which present in JCommander): support for args in format -argname=argvalue

    I change a little CmdLineParser.java and added some unit tests to give examples

    opened by hutm 4
  • I added a handler for the java 7 Path class

    I added a handler for the java 7 Path class

    I am very interested in contributing to your project, this is the first contribution I have worked on, and while it may be a small one, it will also make a world of difference for people as they move to java 7 and nio2. Please give me any feedback, I am willing to make changes or additional work on this, and hope to provide many contributions in the future.

    Thanks, -Kevin-

    opened by madkrupt 4
  • Allow customized ClassParsers

    Allow customized ClassParsers

    Resolves #131

    I'm working on a large enterprise application codebase, which allows creation of multiple different application entry points for the same codebase. Many of these applications share base command line parameters, but different apps have different required fields vs optional fields. There are a large number of command line arguments, so it would be useful in our particular case to be able to override them. This PR adds the ability to use a different version of ClassParser, and provides a new ClassParser subclass which allows subclasses to override arg4j annotations on parent methods/fields.

    opened by alapins 0
  • Fix broken links to java.net

    Fix broken links to java.net

    Hey I noticed this broken link on http://args4j.kohsuke.org/ and that you had fixed it in the README by pointing to the wayback machine. Guessing that the site is statically generated, here's an update to the links

    opened by adamburkegh 0
  • Cannot find tools.jar

    Cannot find tools.jar

    Hi, when using this project as a dependecy in another project it tells me that tools.jar cannot be found. [ERROR] Failed to execute goal on project ***: Could not resolve dependencies for project com.***.plugins:commit-pipeline-xml:hpi:0.1: Could not find artifact jdk:tools:jar:5.0 at specified path /usr/local/java/jdk-14.0.2/../lib/tools.jar -> [Help 1]

    Is there a solution to this?

    opened by agentsmith29 0
  • Eliminate JDK internal classes in unit tests

    Eliminate JDK internal classes in unit tests

    Replace the non-standard NotImplementedException with UnsupportedOperationException.

    At Google, we are upgrading our standard JVM and will no longer have access to sun.reflect.generics.reflectiveObjects.NotImplementedException. This ensures we will be able to run the tests in the future.

    opened by jacksondt 0
  • Also parse environment variables

    Also parse environment variables

    Could it be a good addition to be able to also parse environment variables?

    For example, you could have a program in which you could specify the host to connect to with a --host 127.0.0.1 or with an environment variable "HOST" with the value "127.0.0.1". This could help when it used in places where they commonly use environment variables like in Helm / Kubernetes.

    Nowadays, if you want this behavior you should implement both separately so you can have a problem of not synchronizing it when they change and duplicate validation code, etc.

    Could it be a good idea?

    opened by PhoneixS 0
Owner
Kohsuke Kawaguchi
Kohsuke Kawaguchi