org.unix4j.unix.grep.GrepOption Maven / Gradle / Ivy
package org.unix4j.unix.grep;
import java.util.Collections;
import java.util.EnumSet;
import java.util.Iterator;
import org.unix4j.option.Option;
import org.unix4j.unix.Grep;
/**
* Options for the {@link Grep grep} command.
*
* For most applications, it may be more convenient to use {@link Grep#Options}
* instead of the option constants defined here.
*
*
* {@code -i} {@code --ignoreCase} Match lines ignoring the case when comparing the strings, also known
from Unix with its acronym 'i'.
* {@code -v} {@code --invertMatch} Invert the match result, that is, a non-matching line is written to
the output and a matching line is not. This option is also known
from Unix with its acronym 'v'.
* {@code -F} {@code --fixedStrings} Use fixed-strings matching instead of regular expressions. This is
usually faster than the standard regexp version.
(This option is ignored if a {@code pattern} operand is specified
instead of the {@code regexp} string).
* {@code -n} {@code --lineNumber} Prefix each line of output with the line number within its input
file.
* {@code -c} {@code --count} Suppress normal output; instead print a count of matching lines for
each input file. With the {@code -v}, {@code --invertMatch} option,
count non-matching lines.
* {@code -l} {@code --matchingFiles} Suppress normal output; instead print the name of each input file
from which output would normally have been printed. The scanning
will stop on the first match.
* {@code -x} {@code --wholeLine} Select only those matches that exactly match the whole line
excluding the terminating line ending.
(This option is ignored if a {@code pattern} operand is specified
instead of the {@code regexp} string).
*
*/
public enum GrepOption implements Option, GrepOptions {
/**
* Option {@code --ignoreCase}, {@code -i}:
* Match lines ignoring the case when comparing the strings, also known
from Unix with its acronym 'i'.
*/
ignoreCase('i'),
/**
* Option {@code --invertMatch}, {@code -v}:
* Invert the match result, that is, a non-matching line is written to
the output and a matching line is not. This option is also known
from Unix with its acronym 'v'.
*/
invertMatch('v'),
/**
* Option {@code --fixedStrings}, {@code -F}:
* Use fixed-strings matching instead of regular expressions. This is
usually faster than the standard regexp version.
(This option is ignored if a {@code pattern} operand is specified
instead of the {@code regexp} string).
*/
fixedStrings('F'),
/**
* Option {@code --lineNumber}, {@code -n}:
* Prefix each line of output with the line number within its input
file.
*/
lineNumber('n'),
/**
* Option {@code --count}, {@code -c}:
* Suppress normal output; instead print a count of matching lines for
each input file. With the {@code -v}, {@code --invertMatch} option,
count non-matching lines.
*/
count('c'),
/**
* Option {@code --matchingFiles}, {@code -l}:
* Suppress normal output; instead print the name of each input file
from which output would normally have been printed. The scanning
will stop on the first match.
*/
matchingFiles('l'),
/**
* Option {@code --wholeLine}, {@code -x}:
* Select only those matches that exactly match the whole line
excluding the terminating line ending.
(This option is ignored if a {@code pattern} operand is specified
instead of the {@code regexp} string).
*/
wholeLine('x');
private final char acronym;
private GrepOption(char acronym) {
this.acronym = acronym;
}
@Override
public Class optionType() {
return GrepOption.class;
}
/**
* Returns the option with the given {@code acronym}, or {@code null} if no
* such option is found.
*
* @param acronym the option {@link #acronym() acronym}
* @return the option with the given {@code acronym} or {@code null} if it
* is not found
*/
public static GrepOption findByAcronym(char acronym) {
for (final GrepOption opt : values()) {
if (opt.acronym() == acronym) return opt;
}
return null;
}
@Override
public char acronym() {
return acronym;
}
@Override
public boolean isSet(GrepOption option) {
return equals(option);
}
/**
* Returns a new set with {@code this} active option.
*
* @return a new set containing this option
*/
@Override
public EnumSet asSet() {
return EnumSet.of(this);
}
/**
* Returns an immutable iterator returning o single element: {@code this}
* option.
*
* @return an immutable iterator with {@code this} active option.
*/
@Override
public Iterator iterator() {
return Collections.singleton(this).iterator();
}
/**
* Returns 1 as this is a set with a single element: {@code this} option
*
* @return one
*/
@Override
public int size() {
return 1;
}
/**
* Returns true if the {@link Option#acronym() acronym} should be used for
* the specified {@code option} in string representations.
*
* This method returns always true for all options.
*
* @param option
* the option of interest
* @return always true indicating that option acronyms should be used in
* string representations for all options
*/
@Override
public boolean useAcronymFor(GrepOption option) {
return true;
}
}