Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow the dot character in options and arguments #55

Merged
merged 1 commit into from
May 9, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/main/kotlin/com/xenomachina/argparser/ArgParser.kt
Original file line number Diff line number Diff line change
Expand Up @@ -617,8 +617,8 @@ private val NAME_EQUALS_VALUE_REGEX = Regex("^([^=]+)=(.*)$")
internal val LEADING_HYPHENS_REGEX = Regex("^-{1,2}")

private const val OPTION_CHAR_CLASS = "[a-zA-Z0-9]"
internal val OPTION_NAME_RE = Regex("^(-$OPTION_CHAR_CLASS)|(--$OPTION_CHAR_CLASS+([-_]$OPTION_CHAR_CLASS+)*)$")
internal val OPTION_NAME_RE = Regex("^(-$OPTION_CHAR_CLASS)|(--$OPTION_CHAR_CLASS+([-_\\.]$OPTION_CHAR_CLASS+)*)$")

private const val ARG_INITIAL_CHAR_CLASS = "[A-Z]"
private const val ARG_CHAR_CLASS = "[A-Z0-9]"
internal val ARG_NAME_RE = Regex("^$ARG_INITIAL_CHAR_CLASS+([-_]$ARG_CHAR_CLASS+)*$")
internal val ARG_NAME_RE = Regex("^$ARG_INITIAL_CHAR_CLASS+([-_\\.]$ARG_CHAR_CLASS+)*$")
39 changes: 39 additions & 0 deletions src/test/kotlin/com/xenomachina/argparser/ArgParserTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ class OptionNameValidationTest : Test({
parser.option<Int>("--5", help = TEST_HELP) { 0 }
parser.option<Int>("--5Y", help = TEST_HELP) { 0 }
parser.option<Int>("--X5", help = TEST_HELP) { 0 }
parser.option<Int>("--x.y", help = TEST_HELP) { 0 }

shouldThrow<IllegalArgumentException> {
parser.option<Int>("-_", help = TEST_HELP) { 0 }
Expand Down Expand Up @@ -103,6 +104,18 @@ class OptionNameValidationTest : Test({
shouldThrow<IllegalArgumentException> {
parser.option<Int>("--f!oobar", help = TEST_HELP) { 0 }
}

shouldThrow<IllegalArgumentException> {
parser.option<Int>("--.", help = TEST_HELP) { 0 }
}

shouldThrow<IllegalArgumentException> {
parser.option<Int>("--.foo", help = TEST_HELP) { 0 }
}

shouldThrow<IllegalArgumentException> {
parser.option<Int>("--foo.", help = TEST_HELP) { 0 }
}
})

class PositionalNameValidationTest : Test({
Expand All @@ -113,6 +126,7 @@ class PositionalNameValidationTest : Test({
parser.positional<Int>("XYZ", help = TEST_HELP) { 0 }
parser.positional<Int>("XY-Z", help = TEST_HELP) { 0 }
parser.positional<Int>("XY_Z", help = TEST_HELP) { 0 }
parser.positional<Int>("XY.Z", help = TEST_HELP) { 0 }

shouldThrow<IllegalArgumentException> {
parser.positional<Int>("-", help = TEST_HELP) { 0 }
Expand Down Expand Up @@ -150,6 +164,18 @@ class PositionalNameValidationTest : Test({
parser.positional<Int>("5", help = TEST_HELP) { 0 }
}

shouldThrow<IllegalArgumentException> {
parser.positional<Int>(".", help = TEST_HELP) { 0 }
}

shouldThrow<IllegalArgumentException> {
parser.positional<Int>(".XY", help = TEST_HELP) { 0 }
}

shouldThrow<IllegalArgumentException> {
parser.positional<Int>("XY.", help = TEST_HELP) { 0 }
}

// This should be acceptable
parser.option<Int>("--foobar", argNames = listOf("X-Y"), help = TEST_HELP) { 0 }

Expand Down Expand Up @@ -277,6 +303,19 @@ class ArglessLongOptionsTest : Test({
Args(parserOf("--xray", "--yellow", "--zebra")).xyz shouldBe listOf("--xray", "--yellow", "--zebra")
})

class DottedLongOptionsTest : Test({
class Args(parser: ArgParser) {
val xyz by parser.option<MutableList<String>>("--x.ray", "--color.yellow", "--animal.zebra",
help = TEST_HELP) {
value.orElse { mutableListOf<String>() }.apply {
add("$optionName")
}
}
}

Args(parserOf("--x.ray", "--animal.zebra", "--color.yellow", "--x.ray")).xyz shouldBe listOf("--x.ray", "--animal.zebra", "--color.yellow", "--x.ray")
})

class LongOptionsWithOneArgTest : Test({
class Args(parser: ArgParser) {
val xyz by parser.option<MutableList<String>>("--xray", "--yellow", "--zaphod",
Expand Down