001package org.unix4j.unix.xargs;
002
003import java.util.Collections;
004import java.util.EnumSet;
005import java.util.Iterator;
006
007import org.unix4j.option.Option;
008import org.unix4j.unix.Xargs;
009
010/**
011 * Options for the {@link Xargs xargs} command.
012 * <p>
013 * For most applications, it may be more convenient to use {@link Xargs#Options} 
014 * instead of the option constants defined here.
015 * <p>
016 * <table>
017 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -z}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --delimiter0}</td><td>&nbsp;</td><td>Input items are terminated by a null character instead of by 
018                        whitespace, and the quotes and backslash are not special (every
019                        character is taken literally). Disables the end of file string,
020                        which is treated like any other argument. Useful when input items 
021                        might contain white space, quote marks, or backslashes. The find 
022                        --print0 option produces input suitable for this mode.
023                        <p>
024                        (This option is ignored if an explicit delimiter operand is specified).</td></tr>
025 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -x}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --exactArgs}</td><td>&nbsp;</td><td>Terminate immediately if {@code maxArgs} is specified but the found
026                        number of variable items is less than {@code maxArgs}.          
027<p>
028                        (This option is ignored if no {@code maxArgs} operand is specified).</td></tr>
029 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -r}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --noRunIfEmpty}</td><td>&nbsp;</td><td>If the standard input does not contain any nonblanks, do not run the
030                        command. Normally, the command is run once even if there is no 
031                        input.</td></tr>
032 * <tr valign="top"><td width="10px"></td><td nowrap="nowrap">{@code -t}</td><td>&nbsp;&nbsp;</td><td nowrap="nowrap">{@code --verbose}</td><td>&nbsp;</td><td>Print the command line on the standard error output before executing
033                        it.</td></tr>
034 * </table>
035 */
036public enum XargsOption implements Option, XargsOptions {
037        /**
038         * Option <b>{@code --delimiter0}</b>, <b>{@code -z}</b>: 
039         * Input items are terminated by a null character instead of by 
040                        whitespace, and the quotes and backslash are not special (every
041                        character is taken literally). Disables the end of file string,
042                        which is treated like any other argument. Useful when input items 
043                        might contain white space, quote marks, or backslashes. The find 
044                        --print0 option produces input suitable for this mode.
045                        <p>
046                        (This option is ignored if an explicit delimiter operand is specified).
047         */
048        delimiter0('z'),
049        /**
050         * Option <b>{@code --exactArgs}</b>, <b>{@code -x}</b>: 
051         * Terminate immediately if {@code maxArgs} is specified but the found
052                        number of variable items is less than {@code maxArgs}.          
053<p>
054                        (This option is ignored if no {@code maxArgs} operand is specified).
055         */
056        exactArgs('x'),
057        /**
058         * Option <b>{@code --noRunIfEmpty}</b>, <b>{@code -r}</b>: 
059         * If the standard input does not contain any nonblanks, do not run the
060                        command. Normally, the command is run once even if there is no 
061                        input.
062         */
063        noRunIfEmpty('r'),
064        /**
065         * Option <b>{@code --verbose}</b>, <b>{@code -t}</b>: 
066         * Print the command line on the standard error output before executing
067                        it.
068         */
069        verbose('t');
070        
071        private final char acronym;
072        private XargsOption(char acronym) {
073                this.acronym = acronym;
074        }
075        @Override
076        public Class<XargsOption> optionType() {
077                return XargsOption.class;
078        }
079        /**
080         * Returns the option with the given {@code acronym}, or {@code null} if no
081         * such option is found.
082         * 
083         * @param acronym the option {@link #acronym() acronym}
084         * @return      the option with the given {@code acronym} or {@code null} if it
085         *                      is not found
086         */
087        public static XargsOption findByAcronym(char acronym) {
088                for (final XargsOption opt : values()) {
089                        if (opt.acronym() == acronym) return opt;
090                }
091                return null;
092        }
093        @Override
094        public char acronym() {
095                return acronym;
096        }
097        @Override
098        public boolean isSet(XargsOption option) {
099                return equals(option);
100        }
101        /**
102         * Returns a new set with {@code this} active option.
103         * 
104         * @return a new set containing this option
105         */
106        @Override
107        public EnumSet<XargsOption> asSet() {
108                return EnumSet.of(this);
109        }
110        
111        /**
112         * Returns an immutable iterator returning o single element: {@code this} 
113         * option.
114         * 
115         * @return an immutable iterator with {@code this} active option.
116         */
117        @Override
118        public Iterator<XargsOption> iterator() {
119                return Collections.singleton(this).iterator();
120        }
121        
122        /**
123         * Returns 1 as this is a set with a single element: {@code this} option
124         * 
125         * @return one
126         */
127        @Override
128        public int size() {
129                return 1;
130        }
131
132        /**
133         * Returns true if the {@link Option#acronym() acronym} should be used for
134         * the specified {@code option} in string representations. 
135         * <p>
136         * This method returns always true for all options.
137         *  
138         * @param option
139         *            the option of interest
140         * @return always true indicating that option acronyms should be used in
141         *                      string representations for all options
142         */
143        @Override
144        public boolean useAcronymFor(XargsOption option) {
145                return true;
146        }
147}