/** * argparser – command line argument parser library * * Copyright © 2013 Mattias Andrée (maandree@member.fsf.org) * * This library is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this library. If not, see . */ import java.util.*; import java.io.*; /** * Simple argument parser * * @author Mattias Andrée, maandree@member.fsf.org */ public class ArgParser { /** *

Constructor

*

* The short description is printed on same line as the program name *

* * @param description Short, single-line, description of the program * @param usage Formated, multi-line, usage text, may be {@code null} */ public ArgParser(final String description, final String usage) { this(description, usage, null, null, false); } /** *

Constructor

*

* The short description is printed on same line as the program name *

* * @param description Short, single-line, description of the program * @param usage Formated, multi-line, usage text, may be {@code null} * @param useStderr Whether to use stderr instead of stdout */ public ArgParser(final String description, final String usage, final boolean useStderr) { this(description, usage, null, null, useStderr); } /** *

Constructor

*

* The short description is printed on same line as the program name *

* * @param description Short, single-line, description of the program * @param usage Formated, multi-line, usage text, may be {@code null} * @param longDescription Long, multi-line, description of the program, may be {@code null} */ public ArgParser(final String description, final String usage, final String longDescription) { this(description, usage, longDescription, null, false); } /** *

Constructor

*

* The short description is printed on same line as the program name *

* * @param description Short, single-line, description of the program * @param usage Formated, multi-line, usage text, may be {@code null} * @param longDescription Long, multi-line, description of the program, may be {@code null} * @param useStderr Whether to use stderr instead of stdout */ public ArgParser(final String description, final String usage, final String longDescription, final boolean useStderr) { this(description, usage, longDescription, null, useStderr); } /** *

Constructor

*

* The short description is printed on same line as the program name *

* * @param description Short, single-line, description of the program * @param usage Formated, multi-line, usage text, may be {@code null} * @param longDescription Long, multi-line, description of the program, may be {@code null} * @param program The name of the program, {@code null} for automatic */ public ArgParser(final String description, final String usage, final String longDescription, final String program) { this(description, usage, longDescription, program, false); } /** *

Constructor

*

* The short description is printed on same line as the program name *

* * @param description Short, single-line, description of the program * @param usage Formated, multi-line, usage text, may be {@code null} * @param longDescription Long, multi-line, description of the program, may be {@code null} * @param program The name of the program, {@code null} for automatic * @param useStderr Whether to use stderr instead of stdout */ public ArgParser(final String description, final String usage, final String longDescription, final String program, final boolean useStderr) { this.linuxvt = System.getenv("TERM") == null ? false : System.getenv("TERM").equals("linux"); String prog = program == null ? ArgParser.parentName(0, true) : program; this.program = prog == null ? "?" : prog; this.description = description; this.usage = usage; this.longDescription = longDescription; this.out = useStderr ? System.err : System.out; } /** * Whether the Linux VT is being used */ public boolean linuxvt; /** * The name of the executed command */ public final String program; /** * Short, single-line, description of the program */ private final String description; /** * Formated, multi-line, usage text, {@code null} if none */ private final String usage; /** * Long, multi-line, description of the program, {@code null} if none */ private final String longDescription; /** * The error output stream */ private final OutputStream out; /** * The passed arguments */ public String[] arguments = null; /** * The number of unrecognised arguments */ public int unrecognisedCount = 0; /** * Options, in order */ private final ArrayList