6 from optparse
import OptionParser
, Option
, OptionValueError
8 def _check_file(option
, opt
, value
):
9 """Checks whether value is a valid file and returns it
12 return check_file(value
)
15 def check_file(value
, relative
=False):
16 """Check whether value is a valid file and returns it
21 file = git
.ls_files("--full-name", "--", value
, with_keep_cwd
=relative
)
24 raise OptionValueError("Unknown file '" + value
+ "'")
26 splitfile
= file.split('\n')
28 if len(splitfile
) > 1:
29 raise OptionValueError("Please specify only one file, '" + value
+ "' matches more than one file")
33 def _check_commit(option
, opt
, value
):
34 """Checks whether value is a valid rev and returns its hash
38 rev
= git
.rev_parse("--verify", value
, with_exceptions
=False)
41 raise OptionValueError("Unknown commit '" + value
+ "'")
45 def _check_bool(option
, opt
, value
):
46 """Checks whether a value is a boolean and returns it's value
48 Understands about 'True', 'False', and 'None.
60 raise OptionValueError("Not a boolean: '" + value
+ "'")
62 class GitOption(Option
):
63 """This parser understands a new type, "commit"
66 TYPES
= Option
.TYPES
+ ("commit",) + ("file",) + ("bool",)
67 TYPE_CHECKER
= copy
.copy(Option
.TYPE_CHECKER
)
68 TYPE_CHECKER
["commit"] = _check_commit
69 TYPE_CHECKER
["file"] = _check_file
70 TYPE_CHECKER
["bool"] = _check_bool
72 def isUnique(options
, atLeastOne
=False):
73 """Checks if a list of options is unique
76 options: The list of options to check
77 atLeastOne: If set, when no optiosn are set, return False
82 for option
in options
:
84 # If we already found one, it's not unique for sure
90 # If there is only one, it's unique
94 # None found, so unique only if we don't require at least one