44import java .io .FileReader ;
55import java .io .IOException ;
66import java .io .InputStreamReader ;
7+ import java .io .PrintStream ;
78import java .util .LinkedList ;
89import java .util .List ;
910import java .util .regex .Matcher ;
@@ -27,7 +28,7 @@ protected static Grep parseArguments(String[] args) throws Exception {
2728 g .setGroup (Integer .valueOf (arg .substring (2 )));
2829 } else if (arg .matches ("--group=\\ d+" )) {
2930 g .setGroup (Integer .valueOf (arg .substring (8 )));
30- } else if (arg .matches ("-o" )) {
31+ } else if (arg .matches ("-o" ) || arg . equals ( "--only-matching" ) ) {
3132 g .setGroup (0 );
3233 } else if (g .getRegex () == null ) {
3334 g .setRegex (Pattern .compile (arg )); // throws exception if invalid regex
@@ -41,15 +42,36 @@ protected static Grep parseArguments(String[] args) throws Exception {
4142
4243 public static void main (String [] args ) throws Exception {
4344 if (args .length < 1 ) {
44- System .out . println ( "Usage: jgrep [-v] [-kPOS] [-gGROUP] [-o] PATTERN [FILE]..." );
45+ printHelp ( System .err );
4546 System .exit (1 );
47+ } else if (args [0 ].equals ("-h" ) || args [0 ].equals ("-?" ) || args [0 ].equals ("--help" )) {
48+ printHelp (System .out );
49+ System .exit (0 );
4650 }
4751
4852 Grep g = parseArguments (args );
4953 g .doTheGrepThing ();
5054 }
5155
52- protected boolean invert ;
56+ protected static void printHelp (PrintStream out ) {
57+ out .println ("Usage:" );
58+ out .println (" jgrep [-v] [-kPOS] [-gGROUP] [-o] PATTERN [FILE]..." );
59+ out .println ();
60+ out .println ("Sort of like grep(1), but using java regular expressions, and with a couple of" );
61+ out .println ("unique options." );
62+ out .println ();
63+ out .println ("Options:" );
64+ out .println (" -v, --invert-match Select non-matching lines" );
65+ out .println (" -k, --key=POS Match the given whitespace-delimited field, somewhat" );
66+ out .println (" analogous to sort -k" );
67+ out .println (" -g, --group=GROUP Print the value of the specified capturing group," );
68+ out .println (" instead of the whole matching line" );
69+ out .println (" -o, --only-matching Print only the part of a matching line that matches," );
70+ out .println (" equivalent to --group=0" );
71+ out .println (" -h, --help" );
72+ }
73+
74+ protected boolean invert ;
5375 protected Pattern regex ;
5476 protected List <String > files ;
5577 protected Integer field ;
0 commit comments