1 package org.apache.maven.wrapper.cli;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.Collection;
24 import java.util.HashMap;
25 import java.util.HashSet;
26 import java.util.List;
27 import java.util.Map;
28 import java.util.Set;
29
30
31
32
33 public class ParsedCommandLine
34 {
35 private final Map<String, ParsedCommandLineOption> optionsByString = new HashMap<String, ParsedCommandLineOption>();
36
37 private final Set<String> presentOptions = new HashSet<String>();
38
39 private final List<String> extraArguments = new ArrayList<String>();
40
41 ParsedCommandLine( Iterable<CommandLineOption> options )
42 {
43 for ( CommandLineOption option : options )
44 {
45 ParsedCommandLineOption parsedOption = new ParsedCommandLineOption();
46 for ( String optionStr : option.getOptions() )
47 {
48 optionsByString.put( optionStr, parsedOption );
49 }
50 }
51 }
52
53 @Override
54 public String toString()
55 {
56 return String.format( "options: %s, extraArguments: %s", quoteAndJoin( presentOptions ),
57 quoteAndJoin( extraArguments ) );
58 }
59
60 private String quoteAndJoin( Iterable<String> strings )
61 {
62 StringBuilder output = new StringBuilder();
63 boolean isFirst = true;
64 for ( String string : strings )
65 {
66 if ( !isFirst )
67 {
68 output.append( ", " );
69 }
70 output.append( "'" );
71 output.append( string );
72 output.append( "'" );
73 isFirst = false;
74 }
75 return output.toString();
76 }
77
78
79
80
81
82
83
84 public boolean hasOption( String option )
85 {
86 option( option );
87 return presentOptions.contains( option );
88 }
89
90
91
92
93
94
95
96 public boolean hasAnyOption( Collection<String> logLevelOptions )
97 {
98 for ( String option : logLevelOptions )
99 {
100 if ( hasOption( option ) )
101 {
102 return true;
103 }
104 }
105 return false;
106 }
107
108
109
110
111
112
113
114 public ParsedCommandLineOption option( String option )
115 {
116 ParsedCommandLineOption parsedOption = optionsByString.get( option );
117 if ( parsedOption == null )
118 {
119 throw new IllegalArgumentException( String.format( "Option '%s' not defined.", option ) );
120 }
121 return parsedOption;
122 }
123
124 public List<String> getExtraArguments()
125 {
126 return extraArguments;
127 }
128
129 void addExtraValue( String value )
130 {
131 extraArguments.add( value );
132 }
133
134 ParsedCommandLineOption addOption( String optionStr, CommandLineOption option )
135 {
136 ParsedCommandLineOption parsedOption = optionsByString.get( optionStr );
137 presentOptions.addAll( option.getOptions() );
138 return parsedOption;
139 }
140 }