-
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Return ArgumentCollection from parse
This separates the definition of options (what's available to a user) from the associated arguments (what's provided by the user). Included documentation on public methods. See #2
- Loading branch information
1 parent
f5e0320
commit d542dca
Showing
13 changed files
with
240 additions
and
82 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
#Mon Dec 05 21:26:08 EST 2016 | ||
#Sun Dec 11 20:37:03 EST 2016 | ||
distributionBase=GRADLE_USER_HOME | ||
distributionPath=wrapper/dists | ||
zipStoreBase=GRADLE_USER_HOME | ||
zipStorePath=wrapper/dists | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2-all.zip | ||
distributionUrl=https\://services.gradle.org/distributions/gradle-3.2-bin.zip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 14 additions & 6 deletions
20
kopper-typed/src/main/kotlin/us/jimschubert/kopper/typed/TypedArgumentParser.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,21 +1,29 @@ | ||
package us.jimschubert.kopper.typed | ||
|
||
import us.jimschubert.kopper.ArgumentCollection | ||
import us.jimschubert.kopper.Parser | ||
|
||
/** | ||
* A base type for object-oriented argument parsing. | ||
* | ||
* Use delegated properties defined by, e.g. [BooleanArgument], [NumericArgument], [StringArgument] | ||
*/ | ||
abstract class TypedArgumentParser(val args: Array<String>) { | ||
private var hasParsed = false | ||
private var arguments: ArgumentCollection? = null | ||
val self: TypedArgumentParser by lazy { this } | ||
internal val parser = Parser() | ||
internal fun ensureParsed() { | ||
if(!hasParsed) { | ||
parser.parse(args) | ||
hasParsed = true | ||
internal fun ensureParsed() : ArgumentCollection { | ||
if(arguments == null) { | ||
arguments = parser.parse(args) | ||
} | ||
return arguments!! | ||
} | ||
|
||
fun printHelp() : String { | ||
return parser.printHelp() | ||
} | ||
|
||
val _etc_: List<String> get() = parser.remainingArgs | ||
val _etc_: List<String> get() { | ||
return ensureParsed().unparsedArgs | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package us.jimschubert.kopper | ||
|
||
/** | ||
* Represents an actual value passed via command line arguments, including the option | ||
* responsible for parsing the argument, and the args list that provided options and values. | ||
*/ | ||
data class Argument<out T>(val value: T?, val option: Option<*>, val args: List<String> = listOf()) | ||
|
||
/** | ||
* Merges a newer argument with an older argument | ||
*/ | ||
operator fun <T> Argument<T>.plus(newer: Argument<T>): Argument<T> { | ||
return copy(value = newer.value, args = args + newer.args) | ||
} |
25 changes: 25 additions & 0 deletions
25
kopper/src/main/kotlin/us/jimschubert/kopper/ArgumentCollection.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package us.jimschubert.kopper | ||
|
||
/** | ||
* Represents a collection of arguments provided by the user. | ||
* | ||
* Includes helper methods for querying by option name (short or long). | ||
* | ||
* @note Maintains a bucket of unparsed arguments | ||
*/ | ||
class ArgumentCollection(val unparsedArgs: List<String>, | ||
parsedArgumentList: List<Argument<*>> | ||
) : List<Argument<*>> by parsedArgumentList { | ||
fun option(name: String): String? { | ||
val found: Argument<*>? = find { (it.option.shortOption == name || it.option.longOption.contains(name)) } | ||
return (found?.value as? String?) | ||
} | ||
|
||
fun flag(name: String): Boolean { | ||
val found = find { | ||
it.option.isFlag && (it.option.shortOption == name || it.option.longOption.contains(name)) | ||
} ?: return false | ||
|
||
return found.value as? Boolean ?: false | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 5 additions & 2 deletions
7
kopper/src/main/kotlin/us/jimschubert/kopper/StringExtensions.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.