Skip to content

Latest commit

 

History

History
93 lines (72 loc) · 2.44 KB

README.md

File metadata and controls

93 lines (72 loc) · 2.44 KB

Gnopt

Copyright © 2019, 2022, Christopher Alan Mosher, Shelton, Connecticut, USA, cmosher01@gmail.com.

Donate License

Gnopt is a command-line option processor for Java programs. It handles GNU-style options:

--option=value

This software is distributed under the GPLv3 license.

Include as a dependency in gradle:

repositories {
    mavenCentral()
    maven {
        url = uri('https://public:\u0067hp_fya6Kseu3XOBMg2icbg7f1LP6ZFYjj35v4Zj@maven.pkg.github.com/cmosher01/*')
    }
}

dependencies {
    implementation group: 'nu.mine.mosher.gnopt', name: 'gnopt', version: 'latest.release'
}

Example:

package demo;

import nu.mine.mosher.gnopt.Gnopt;

import java.util.Optional;

/**
 * Command line option processor.
 * This is where you define and handle all your command-line options.
 *
 * It is just a POJO.
 * Each public, non-static, method becomes a command line option.
 *
 * Method requirements:
 *
 *     1. must be public
 *     2. must have void return type
 *     3. must have one and only one Optional<String> argument
 *     4. cannot be abstract
 *
 *  Turn on logging (slf4j) to see any error messages.
 */
@SuppressWarnings({"unused", "OptionalUsedAsFieldOrParameterType"})
public class FoobarOpts {
    // --something=whatever
    public void something(Optional<String> val) {
        foo = val.orElse("");
    }

    // non-option arguments call this method (two underscores)
    public void __(Optional<String> val) {
        System.err.println("arg: "+val.get());
    }

    // you can store configuration state in instance variables
    public String foo = "default";



    // The main program can be anywhere; it doesn't need to
    // be in this class.
    public static void main(String[] args) throws Throwable {
        // Pass args into Gnopt, and it will create a new instance
        // and call methods on it for each argument:
        FoobarOpts opts = Gnopt.process(FoobarOpts.class, args);

        // then use opts as you wish within the rest of your program
        System.err.println("something: "+opts.foo);
    }
}
java -cp ... demo.Demo   --something=testing infile
arg: infile
something: testing