@@ -57,6 +57,12 @@ class ArgParser(args: Array<out String>,
5757 isRepeating = false ,
5858 help = help) { true }.default(false )
5959
60+ /* *
61+ * Creates a DelegateProvider for a zero-argument option that returns true if and only the option is present in args.
62+ */
63+ fun flagging (help : String ) =
64+ DelegateProvider { name -> flagging(identifierToOptionName(name), help = help) }
65+
6066 /* *
6167 * Creates a Delegate for a zero-argument option that returns the count of how many times the option appears in args.
6268 */
@@ -68,6 +74,11 @@ class ArgParser(args: Array<out String>,
6874 isRepeating = true ,
6975 help = help) { value.orElse { 0 } + 1 }.default(0 )
7076
77+ /* *
78+ * Creates a DelegateProvider for a zero-argument option that returns the count of how many times the option appears in args.
79+ */
80+ fun counting (help : String ) = DelegateProvider { name -> counting(identifierToOptionName(name), help = help) }
81+
7182 /* *
7283 * Creates a Delegate for a single-argument option that stores and returns the option's (transformed) argument.
7384 */
@@ -85,12 +96,25 @@ class ArgParser(args: Array<out String>,
8596 help = help) { transform(this .next()) }
8697 }
8798
99+ /* *
100+ * Creates a DelegateProvider for a single-argument option that stores and returns the option's (transformed) argument.
101+ */
102+ fun <T > storing (
103+ help : String ,
104+ transform : String .() -> T
105+ ) = DelegateProvider { name -> storing(identifierToOptionName(name), help = help, transform = transform) }
106+
88107 /* *
89108 * Creates a Delegate for a single-argument option that stores and returns the option's argument.
90109 */
91110 fun storing (vararg names : String , help : String ): Delegate <String > =
92111 storing(* names, help = help) { this }
93112
113+ /* *
114+ * Creates a DelegateProvider for a single-argument option that stores and returns the option's argument.
115+ */
116+ fun storing (help : String ) = DelegateProvider { name -> storing(identifierToOptionName(name), help = help) }
117+
94118 /* *
95119 * Creates a Delegate for a single-argument option that adds the option's (transformed) argument to a
96120 * MutableCollection each time the option appears in args, and returns said MutableCollection.
@@ -114,6 +138,17 @@ class ArgParser(args: Array<out String>,
114138 }.default(initialValue)
115139 }
116140
141+ /* *
142+ * Creates a DelegateProvider for a single-argument option that adds the option's (transformed) argument to a
143+ * MutableCollection each time the option appears in args, and returns said MutableCollection.
144+ */
145+ fun <E , T : MutableCollection <E >> adding (
146+ initialValue : T ,
147+ help : String ,
148+ transform : String .() -> E
149+ ) = DelegateProvider { name ->
150+ adding(identifierToOptionName(name), initialValue = initialValue, help = help, transform = transform) }
151+
117152 /* *
118153 * Creates a Delegate for a single-argument option that adds the option's (transformed) argument to a
119154 * MutableList each time the option appears in args, and returns said MutableCollection.
@@ -124,13 +159,30 @@ class ArgParser(args: Array<out String>,
124159 transform : String .() -> T
125160 ) = adding(* names, initialValue = mutableListOf (), help = help, transform = transform)
126161
162+ /* *
163+ * Creates a DelegateProvider for a single-argument option that adds the option's (transformed) argument to a
164+ * MutableList each time the option appears in args, and returns said MutableCollection.
165+ */
166+ fun <T > adding (
167+ help : String ,
168+ transform : String .() -> T
169+ ) = DelegateProvider { name ->
170+ adding(identifierToOptionName(name), help = help, transform = transform) }
171+
127172 /* *
128173 * Creates a Delegate for a single-argument option that adds the option's argument to a MutableList each time the
129174 * option appears in args, and returns said MutableCollection.
130175 */
131176 fun adding (vararg names : String , help : String ): Delegate <MutableList <String >> =
132177 adding(* names, help = help) { this }
133178
179+ /* *
180+ * Creates a DelegateProvider for a single-argument option that adds the option's argument to a MutableList each time the
181+ * option appears in args, and returns said MutableCollection.
182+ */
183+ fun adding (help : String ) = DelegateProvider { name ->
184+ adding(identifierToOptionName(name), help = help) }
185+
134186 /* *
135187 * Creates a Delegate for a zero-argument option that maps from the option's name as it appears in args to one of a
136188 * fixed set of values.
@@ -189,6 +241,12 @@ class ArgParser(args: Array<out String>,
189241 */
190242 fun positional (name : String , help : String ) = positional(name, help = help) { this }
191243
244+ /* *
245+ * Creates a DelegateProvider for a single positional argument which returns the argument's value.
246+ */
247+ fun positional (help : String ) =
248+ DelegateProvider { ident -> positional(identifierToArgName(ident), help = help) }
249+
192250 /* *
193251 * Creates a Delegate for a single positional argument which returns the argument's transformed value.
194252 */
@@ -204,6 +262,14 @@ class ArgParser(args: Array<out String>,
204262 }
205263 }
206264
265+ /* *
266+ * Creates a DelegateProvider for a single positional argument which returns the argument's transformed value.
267+ */
268+ fun <T > positional (
269+ help : String ,
270+ transform : String .() -> T
271+ ) = DelegateProvider { ident -> positional(identifierToArgName(ident), help = help, transform = transform) }
272+
207273 /* *
208274 * Creates a Delegate for a sequence of positional arguments which returns a List containing the arguments.
209275 */
@@ -213,6 +279,14 @@ class ArgParser(args: Array<out String>,
213279 help : String
214280 ) = positionalList(name, sizeRange, help = help) { this }
215281
282+ /* *
283+ * Creates a DelegateProvider for a sequence of positional arguments which returns a List containing the arguments.
284+ */
285+ fun positionalList (
286+ sizeRange : IntRange = 1..Int .MAX_VALUE ,
287+ help : String
288+ ) = DelegateProvider { ident -> positionalList(identifierToArgName(ident), sizeRange, help = help) }
289+
216290 /* *
217291 * Creates a Delegate for a sequence of positional arguments which returns a List containing the transformed
218292 * arguments.
@@ -241,6 +315,16 @@ class ArgParser(args: Array<out String>,
241315 }
242316 }
243317
318+ /* *
319+ * Creates a DelegateProvider for a sequence of positional arguments which returns a List containing the transformed
320+ * arguments.
321+ */
322+ fun <T > positionalList (
323+ sizeRange : IntRange = 1..Int .MAX_VALUE ,
324+ help : String ,
325+ transform : String .() -> T
326+ ) = DelegateProvider { ident -> positionalList(identifierToArgName(ident), sizeRange, help, transform) }
327+
244328 internal abstract class WrappingDelegate <U , W >(private val inner : Delegate <U >) : Delegate<W> {
245329
246330 abstract fun wrap (u : U ): W
@@ -258,7 +342,7 @@ class ArgParser(args: Array<out String>,
258342 override fun default (value : W ): Delegate <W > =
259343 apply { inner.default(unwrap(value)) }
260344
261- override fun addValidtator (validator : Delegate <W >.() -> Unit ): Delegate <W > =
345+ override fun addValidator (validator : Delegate <W >.() -> Unit ): Delegate <W > =
262346 apply { validator(this ) }
263347 }
264348
@@ -281,7 +365,31 @@ class ArgParser(args: Array<out String>,
281365 fun default (value : T ): Delegate <T >
282366
283367 /* * Add validation logic. Validator should throw a [SystemExitException] on failure. */
284- fun addValidtator (validator : Delegate <T >.() -> Unit ): Delegate <T >
368+ fun addValidator (validator : Delegate <T >.() -> Unit ): Delegate <T >
369+
370+ @Deprecated(" Use addValidator instead" )
371+ fun addValidtator (validator : Delegate <T >.() -> Unit ) = addValidator(validator)
372+ }
373+
374+ /* *
375+ * Provides a [Delegate] when given a name. This makes it possible to infer
376+ * a name for the `Delegate` based on the name it is bound to, rather than
377+ * specifying a name explicitly.
378+ */
379+ class DelegateProvider <T >(private val ctor : (ident: String ) -> Delegate <T >) {
380+ operator fun provideDelegate (thisRef : Any? , prop : KProperty <* >): Delegate <T > {
381+ return ctor(prop.name).apply {
382+ defaultValue?.let {
383+ default(it.value)
384+ }
385+ }
386+ }
387+
388+ fun default (t : T ): DelegateProvider <T > = apply {
389+ defaultValue = Holder (t)
390+ }
391+
392+ private var defaultValue : Holder <T >? = null
285393 }
286394
287395 internal abstract class ParsingDelegate <T >(
@@ -305,7 +413,7 @@ class ArgParser(args: Array<out String>,
305413 return this
306414 }
307415
308- override fun addValidtator (validator : Delegate <T >.() -> Unit ): Delegate <T > = apply {
416+ override fun addValidator (validator : Delegate <T >.() -> Unit ): Delegate <T > = apply {
309417 validators.add(validator)
310418 }
311419
@@ -621,6 +729,14 @@ class ArgParser(args: Array<out String>,
621729
622730 private fun optionNameToArgName (name : String ) =
623731 LEADING_HYPHENS .replace(name, " " ).toUpperCase().replace(' -' , ' _' )
732+
733+ internal fun identifierToOptionName (ident : String ) : String {
734+ return if (ident.length == 1 ) (" -" + ident) else (" --" + ident.replace(' _' , ' -' ))
735+ }
736+
737+ internal fun identifierToArgName (ident : String ) : String {
738+ return ident.toUpperCase()
739+ }
624740 }
625741
626742 init {
0 commit comments