MiniCalc Sample

Implements a small commandline application with two subcommands

  • plus gets variable number of integer arguments and computes their sum
  • minus gets two integer arguments and computes their difference

The whole implementation is trivial that it can be stored in a single java file; but I have split it for better readability.

Demonstrates:

  • using variable parameter list (plus)
  • working with floating-point values

Files:

MiniCalc.java

package net.kozelka.args.minicalc;

import net.kozelka.args.BasicArgsParser;
import net.kozelka.args.DefaultHelpCommand;
import net.kozelka.args.annotation.AnnottationAwareSetup;

/**
 * Sample application for <a href="http://code.kozelka.net/args-inject">args-inject</a>
 * @author <a href="mailto:pkozelka@gmail.com">Petr Kozelka</a>
 */
public class MiniCalc {

    // START SNIPPET: run
    /**
     * Highest-possible level of invocation usable both from {@link #main} and from unit tests.
     *
     * @param args commandline arguments
     * @return true if successful
     * @throws Exception -
     */
    static int run(String... args) throws Exception {
        final AnnottationAwareSetup setup = new AnnottationAwareSetup("minicalc");
        setup.addSubCommand(DefaultHelpCommand.class);
        setup.addSubCommand(Plus.class);
        setup.addSubCommand(Minus.class);
        setup.addSubCommand(Logarithm.class);
        return BasicArgsParser.process(setup, args);
    }
    // END SNIPPET: run

    // START SNIPPET: main
    public static void main(String[] args) throws Exception {
        final int exitCode = run(args);
        if (exitCode != 0) {
            System.exit(exitCode);
        }
    }
    // END SNIPPET: main
}

minicalc help

Usage:
   minicalc <subcommand> [options] [args]

Available subcommands:
   help     shows help for the whole app. or for specified command
   log      computes logarithm of given argument
   minus    computes operand1 - operand2
   plus     computes sum of all given numbers

Type 'minicalc help <subcommand>' for help on a specific subcommand

Plus.java

package net.kozelka.args.minicalc;

import net.kozelka.args.annotation.SubCommand;
import net.kozelka.args.api.ArgsCommand;

@SubCommand(name = "plus", description = "computes sum of all given numbers")
public class Plus implements ArgsCommand {
    private final int[] numbers;

    public Plus(int... numbers) {
        this.numbers = numbers;
    }

    public Integer call() throws Exception {
        int sum = 0;
        for (int number : numbers) {
            sum += number;
        }
        System.out.println(sum);
        return 0;
    }
}

minicalc help plus

Subcommand 'plus': computes sum of all given numbers

Usage:
   minicalc plus [options] <int...>

Minus.java

package net.kozelka.args.minicalc;

import net.kozelka.args.annotation.Param;
import net.kozelka.args.annotation.SubCommand;
import net.kozelka.args.api.ArgsCommand;

@SubCommand(name = "minus", description = "computes operand1 - operand2")
public class Minus implements ArgsCommand {
    private final int x;
    private final int y;

    public Minus(@Param("operand1") int x, @Param("operand2") int y) {
        this.x = x;
        this.y = y;
    }

    public Integer call() throws Exception {
        System.out.println(x - y);
        return 0;
    }
}

minicalc help minus

Subcommand 'minus': computes operand1 - operand2

Usage:
   minicalc minus [options] <operand1> <operand2>

Logarithm.java

package net.kozelka.args.minicalc;

import net.kozelka.args.annotation.Option;
import net.kozelka.args.annotation.Param;
import net.kozelka.args.annotation.SubCommand;
import net.kozelka.args.api.ArgsCommand;

@SubCommand(name = "log", description = "computes logarithm of given argument")
public class Logarithm implements ArgsCommand {
    private final double x;
    private double base = 0;

    public Logarithm(@Param("arg") double x) {
        this.x = x;
    }

    @Option(longName = "--base", shortName = "-b", description = "the base of the logarithm")
    public void setBase(@Param("base") double base) {
        this.base = base;
    }

    public Integer call() throws Exception {
        final double rv;
        if (base == 0) {
            // natural logarithm
            rv = Math.log(x);
        } else if (base == 10) {
            rv = Math.log10(x);
        } else {
            rv = Math.log(x) / Math.log(base);
        }
        System.out.println(rv);
        return 0;
    }
}

minicalc help log

Subcommand 'log': computes logarithm of given argument

Usage:
   minicalc log [options] <arg>

Valid options:
   -b [--base] <base>  the base of the logarithm