Navigation Menu

Skip to content

Commit

Permalink
GetOpt with() won't accept arguments of wrong type
Browse files Browse the repository at this point in the history
example.io moved to examples/ directory
  • Loading branch information
superbobry committed Apr 27, 2010
1 parent 6f3a163 commit d24d682
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 4 deletions.
13 changes: 9 additions & 4 deletions GetOpt.io
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-

# This object helps scripts to parse the command line arguments in
# System.args. It supports the same conventions as the Unix getopt()
# System args. It supports the same conventions as the Unix getopt()
# function. Most of the code is ported from the getopt module
# from Python's standart library.

Expand All @@ -16,11 +16,16 @@ Object do(
)

GetOpt := Object clone do(
shortopts ::= ""
longopts ::= list()
init := method(
self shortopts := ""
self longopts := list()
)

with := method(shortopts, longopts,
self clone setLongopts(longopts) setShortopts(shortopts)
getopt := self clone
if(shortopts isKindOf(Sequence), getopt shortopts = shortopts)
if(longopts isKindOf(List), getopt longopts = longopts)
getopt
) doc(
"""
Creates a new option parser object with the arguments given. The
Expand Down
2 changes: 2 additions & 0 deletions example.io → examples/echo.io
@@ -1,6 +1,8 @@
#!/usr/bin/env io
# -*- coding: utf-8 -*-

Importer addSearchPath("..")

Command

echo := command(msg,
Expand Down
32 changes: 32 additions & 0 deletions tests/GetOptTest.io
Expand Up @@ -8,6 +8,38 @@ GetOptTest := UnitTest clone do(
)
)

testInit := method(
getopt := GetOpt clone
assertEquals("", getopt getLocalSlot("shortopts"))
assertEquals(list(), getopt getLocalSlot("longopts"))
)

testWith := method(
# Testing that nil values aren't being assigned.
getopt := GetOpt with
assertEquals("", getopt getLocalSlot("shortopts"))
assertEquals(list(), getopt getLocalSlot("longopts"))

getopt := GetOpt with(nil, nil)
assertEquals("", getopt getLocalSlot("shortopts"))
assertEquals(list(), getopt getLocalSlot("longopts"))

getopt := GetOpt with(nil, list("xyz"))
assertEquals("", getopt getLocalSlot("shortopts"))
assertEquals(list("xyz"), getopt getLocalSlot("longopts"))

getopt := GetOpt with("x", nil)
assertEquals("x", getopt getLocalSlot("shortopts"))
assertEquals(list(), getopt getLocalSlot("longopts"))

# Testing that values of the wrong type aren't being
# assigned as well.
getopt := GetOpt with(list(), "")
assertEquals("", getopt getLocalSlot("shortopts"))
assertEquals(list(), getopt getLocalSlot("longopts"))

)

testShortHasArg := method(
assertTrue(getopt shortHasArg("a"))
assertTrue(getopt shortHasArg("b"))
Expand Down

0 comments on commit d24d682

Please sign in to comment.