001package org.unix4j.variable;
002
003/**
004 * The Arg class defines variable name constants for command arguments.
005 */
006public class Arg {
007        private static final String PREFIX = "$";
008        private static final String ALL = "$@";
009        private static final String FROM_PREFIX = "$@+";
010
011        /**
012         * Name for the variable referring to the first command line argument (zero
013         * based index)
014         */
015        public static final String $0 = arg(0);
016        /**
017         * Name for the variable referring to the second command line argument (zero
018         * based index)
019         */
020        public static final String $1 = arg(1);
021        /**
022         * Name for the variable referring to the third command line argument (zero
023         * based index)
024         */
025        public static final String $2 = arg(2);
026        /**
027         * Name for the variable referring to the fourth command line argument (zero
028         * based index)
029         */
030        public static final String $3 = arg(3);
031        /**
032         * Name for the variable referring to the fifth command line argument (zero
033         * based index)
034         */
035        public static final String $4 = arg(4);
036        /**
037         * Name for the variable referring to the sixth command line argument (zero
038         * based index)
039         */
040        public static final String $5 = arg(5);
041        /**
042         * Name for the variable referring to the seventh command line argument
043         * (zero based index)
044         */
045        public static final String $6 = arg(6);
046        /**
047         * Name for the variable referring to the eighth command line argument (zero
048         * based index)
049         */
050        public static final String $7 = arg(7);
051        /**
052         * Name for the variable referring to the ninth command line argument (zero
053         * based index)
054         */
055        public static final String $8 = arg(8);
056        /**
057         * Name for the variable referring to the tenth command line argument (zero
058         * based index)
059         */
060        public static final String $9 = arg(9);
061        /**
062         * Name for the variable referring to all command line arguments.
063         */
064        public static final String $all = ALL;
065
066        /**
067         * Name for the variable referring to the <code>(i+1)<sup>th</sup></code>
068         * command line argument (the index {@code i} is zero based).
069         * 
070         * @param i
071         *            zero based argument index
072         * @return variable name for the <code>(i+1)<sup>th</sup></code> command
073         *         line argument
074         */
075        public static final String arg(int i) {
076                return PREFIX + i;
077        }
078
079        /**
080         * Returns true if and only if the given name starts with the "$" prefix.
081         * 
082         * @param expression
083         *            the potential variable name to check
084         * @return if the first character of expression is a "$" character
085         */
086        public static boolean isVariable(String expression) {
087                return expression.startsWith(PREFIX);
088        }
089
090        /**
091         * Returns the zero based index of an arg variable given the name of the
092         * variable. This method is the antipode of the {@code #arg(int)} method, it
093         * returns -1 if the given name is not compatible with the name returned by
094         * {@link #arg(int)}.
095         * 
096         * @param name
097         *            the name of the variable, such as "$0", "$1" etc.
098         * @return the index of the argument referenced by the variable, for
099         *         instance 0 for the name "$0"
100         */
101        public static int argIndex(String name) {
102                if (name.startsWith(PREFIX)) {
103                        try {
104                                return Integer.parseInt(name.substring(PREFIX.length()));
105                        } catch (NumberFormatException e) {
106                                return -1;
107                        }
108                }
109                return -1;
110        }
111
112        /**
113         * Returns the zero based start index of an args variable given the name of
114         * the variable. This method is the antipode of the {@code #argsFrom(int)}
115         * method, it returns -1 if the given name is not compatible with the name
116         * returned by {@link #argsFrom(int)}.
117         * 
118         * @param name
119         *            the name of the variable, such as "$@+1", "$@+3" etc.
120         * @return the index of the argument referenced by the variable, for
121         *         instance 1 for the name "$@+1"
122         */
123        public static int argsFromIndex(String name) {
124                if (name.startsWith(FROM_PREFIX)) {
125                        try {
126                                return Integer.parseInt(name.substring(FROM_PREFIX.length()));
127                        } catch (NumberFormatException e) {
128                                return -1;
129                        }
130                }
131                return -1;
132        }
133
134        /**
135         * Name for the variable referring to all command line arguments.
136         * 
137         * @return {@link #$all}
138         */
139        public static final String args() {
140                return $all;
141        }
142
143        /**
144         * Name for the variable referring to all command line arguments starting
145         * from the specified {@code index}.
146         * 
147         * @param index
148         *            zero based index of first argument in the args list
149         * @return variable name for the <code>(index+1)<sup>th</sup></code> command
150         *         line argument followed by all other arguments
151         */
152        public static final String argsFrom(int index) {
153                if (index < 0)
154                        throw new IllegalArgumentException("from cannot be negative: " + index);
155                return index == 0 ? $all : (FROM_PREFIX + index);
156        }
157
158        // no instances
159        private Arg() {
160                super();
161        }
162}