1 module eph.args.arg;
2 
3 import eph.args.param: Parameter;
4 
5 /**
6  * CLI Argument / Flag
7  *
8  * Represents a unique cli flag that can have a short and/or long form.
9  */
10 public class Argument {
11 
12   private char sFlag;
13 
14   private string lFlag;
15 
16   private string desc;
17 
18   private bool isRequired;
19 
20   private bool hasParam;
21 
22   private bool isParamRequired;
23 
24   private uint hits;
25 
26   private string[] vals;
27 
28   /**
29    * Sets this argument's short flag.
30    * Returns the current Argument instance.
31    */
32   public Argument shortFlag(char c) {
33     sFlag = c;
34     return this;
35   }
36 
37   /**
38    * Returns this argument's short flag
39    */
40   public char shortFlag() const {
41     return sFlag;
42   }
43 
44   /**
45    * Returns whether or not this argument has a short flag
46    * set.
47    */
48   public bool hasShortFlag() const {
49     return sFlag == 0;
50   }
51 
52   /**
53    * Sets this argument's long flag.
54    * Returns the current Argument instance.
55    */
56   public Argument longFlag(string l) {
57     lFlag = l;
58     return this;
59   }
60 
61   /**
62    * Returns this argument's long flag.
63    */
64   public string longFlag() const {
65     return lFlag;
66   }
67 
68   /**
69    * Returns whether or not this argument has a long flag
70    * set.
71    */
72   public bool hasLongFlag() const {
73     return lFlag != "";
74   }
75 
76   /**
77    * Sets this argument as required.
78    */
79   public Argument require() {
80     isRequired = true;
81     return this;
82   }
83 
84   /**
85    * Returns whether or not this Argument is required.
86    */
87   public bool required() const {
88     return isRequired;
89   }
90 
91   /**
92    * Sets this Argument to require a parameter value.
93    */
94   public Argument requireParam() {
95     hasParam = true;
96     isParamRequired = true;
97     return this;
98   }
99 
100   /**
101    * Whether or not this Argument requires a parameter
102    * value.
103    */
104   public bool paramRequired() const {
105     return isParamRequired;
106   }
107 
108   /**
109    * Sets this Argument to expect an optional value
110    * parameter.
111    */
112   public Argument optionalParam() {
113     hasParam = true;
114     isParamRequired = false;
115     return this;
116   }
117 
118   /**
119    * Returns whether this Argument has a parameter which is
120    * optional.
121    */
122   public bool hasOptionalParam() const {
123     return hasParam && !this.isParamRequired;
124   }
125 
126   /**
127    * Returns whether or not this argument expects a value
128    * parameter.
129    */
130   public bool parameterized() const {
131     return hasParam;
132   }
133 
134   /**
135    * Set this argument's description
136    */
137   public Argument description(string d) {
138     desc = d;
139     return this;
140   }
141 
142   /**
143    * Get this argument's description.
144    */
145   public string description() const {
146     return desc;
147   }
148 
149   /**
150    * Whether or not this argument has a description set.
151    */
152   public bool hasDescription() const {
153     return desc != "";
154   }
155 
156   /**
157    * Get values passed to this argument from each usage on
158    * the command line.
159    */
160   public string[] values() {
161     return vals;
162   }
163 
164   /**
165    * Get number of times this argument was used
166    */
167   public uint uses() const {
168     return hits;
169   }
170 
171   /**
172    * Get whether or not this flag was used
173    */
174   public bool set() const {
175     return hits > 0;
176   }
177 
178   /**
179    * Appends a parameter value to this argument.
180    */
181   package void value(string val) {
182     vals ~= val;
183   }
184 
185   /**
186    * Marks this Argument has having been used.
187    * Additionally increments the internal usage counter.
188    */
189   package void use() {
190     hits++;
191   }
192 }