def test_do_shorts(self): opts, args = getopt.do_shorts([], 'a', 'a', []) self.assertEqual(opts, [('-a', '')]) self.assertEqual(args, []) opts, args = getopt.do_shorts([], 'a1', 'a:', []) self.assertEqual(opts, [('-a', '1')]) self.assertEqual(args, []) opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) self.assertEqual(opts, [('-a', '1')]) self.assertEqual(args, []) opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) self.assertEqual(opts, [('-a', '1')]) self.assertEqual(args, ['2']) self.assertError(getopt.do_shorts, [], 'a1', 'a', []) self.assertError(getopt.do_shorts, [], 'a', 'a:', [])
def getopt_with_ignore(args, shortopts, longopts=[]): """my_getopt(args, options[, long_options]) -> opts, args This function works like gnu_getopt(), except that unknown parameters are ignored rather than raising an error. """ opts = [] prog_args = [] if isinstance(longopts, str): longopts = [longopts] else: longopts = list(longopts) while args: if args[0] == '--': prog_args += args[1:] break if args[0].startswith('--'): try: opts, args = getopt.do_longs(opts, args[0][2:], longopts, args[1:]) except getopt.GetoptError: args = args[1:] elif args[0][0] == '-': try: opts, args = getopt.do_shorts(opts, args[0][1:], shortopts, args[1:]) except getopt.GetoptError: args = args[1:] else: prog_args.append(args[0]) args = args[1:] return opts, prog_args
def my_getopt(args, shortopts, longopts = []): opts = [] prog_args = [] if type(longopts) == type(""): longopts = [longopts] else: longopts = list(longopts) # Allow options after non-option arguments? if shortopts[0] == '+': shortopts = shortopts[1:] all_options_first = 1 elif os.environ.has_key("POSIXLY_CORRECT"): all_options_first = 1 else: all_options_first = 0 while args: if args[0] == '--': prog_args += args[1:] break if args[0][:2] == '--': opts, args = getopt.do_longs(opts, args[0][2:], longopts, args[1:]) elif args[0][:1] == '-': opts, args = getopt.do_shorts(opts, args[0][1:], shortopts, args[1:]) else: if all_options_first: prog_args += args break else: prog_args.append(args[0]) args = args[1:] return opts, prog_args
def getopt_with_ignore(args, shortopts, longopts=[]): """my_getopt(args, options[, long_options]) -> opts, args This function works like gnu_getopt(), except that unknown parameters are ignored rather than raising an error. """ opts = [] prog_args = [] if isinstance(longopts, str): longopts = [longopts] else: longopts = list(longopts) while args: if args[0] == '--': prog_args += args[1:] break if args[0][:2] == '--': try: opts, args = getopt.do_longs(opts, args[0][2:], longopts, args[1:]) except getopt.GetoptError: prog_args.append(args[0]) args = args[1:] elif args[0][:1] == '-': try: opts, args = getopt.do_shorts(opts, args[0][1:], shortopts, args[1:]) except getopt.GetoptError: prog_args.append(args[0]) args = args[1:] else: prog_args.append(args[0]) args = args[1:] return opts, prog_args
def test_do_shorts(self): opts, args = getopt.do_shorts([], 'a', 'a', []) self.assertEqual(opts, [('-a', '')]) self.assertEqual(args, []) opts, args = getopt.do_shorts([], 'a1', 'a:', []) self.assertEqual(opts, [('-a', '1')]) self.assertEqual(args, []) #opts, args = getopt.do_shorts([], 'a=1', 'a:', []) #self.assertEqual(opts, [('-a', '1')]) #self.assertEqual(args, []) opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) self.assertEqual(opts, [('-a', '1')]) self.assertEqual(args, []) opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) self.assertEqual(opts, [('-a', '1')]) self.assertEqual(args, ['2']) self.assertError(getopt.do_shorts, [], 'a1', 'a', []) self.assertError(getopt.do_shorts, [], 'a', 'a:', [])
def test_do_shorts(self): opts, args = getopt.do_shorts([], "a", "a", []) self.assertEqual(opts, [("-a", "")]) self.assertEqual(args, []) opts, args = getopt.do_shorts([], "a1", "a:", []) self.assertEqual(opts, [("-a", "1")]) self.assertEqual(args, []) # opts, args = getopt.do_shorts([], 'a=1', 'a:', []) # self.assertEqual(opts, [('-a', '1')]) # self.assertEqual(args, []) opts, args = getopt.do_shorts([], "a", "a:", ["1"]) self.assertEqual(opts, [("-a", "1")]) self.assertEqual(args, []) opts, args = getopt.do_shorts([], "a", "a:", ["1", "2"]) self.assertEqual(opts, [("-a", "1")]) self.assertEqual(args, ["2"]) self.assertError(getopt.do_shorts, [], "a1", "a", []) self.assertError(getopt.do_shorts, [], "a", "a:", [])
def my_getopt(args, shortopts, longopts=[]): """getopt(args, options[, long_options]) -> opts, args Parses command line options and parameter list. args is the argument list to be parsed, without the leading reference to the running program. Typically, this means "sys.argv[1:]". shortopts is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (i.e., the same format that Unix getopt() uses). If specified, longopts is a list of strings with the names of the long options which should be supported. The leading '--' characters should not be included in the option name. Options which require an argument should be followed by an equal sign ('='). The return value consists of two elements: the first is a list of (option, value) pairs; the second is the list of program arguments left after the option list was stripped (this is a trailing slice of the first argument). Each option-and-value pair returned has the option as its first element, prefixed with a hyphen (e.g., '-x'), and the option argument as its second element, or an empty string if the option has no argument. The options occur in the list in the same order in which they were found, thus allowing multiple occurrences. Long and short options may be mixed. """ opts = [] if type(longopts) == type(""): longopts = [longopts] else: longopts = list(longopts) if args and args[0].startswith('-') and args[0] != '-': if args[0] == '--': args = args[1:] if args[0].startswith('--'): opts, args = getopt.do_longs(opts, args[0][2:], longopts, args[1:]) else: opts, args = getopt.do_shorts(opts, args[0][1:], shortopts, args[1:]) return opts, args else: return None, args
def my_getopt(args, shortopts, longopts = []): """getopt(args, options[, long_options]) -> opts, args Parses command line options and parameter list. args is the argument list to be parsed, without the leading reference to the running program. Typically, this means "sys.argv[1:]". shortopts is the string of option letters that the script wants to recognize, with options that require an argument followed by a colon (i.e., the same format that Unix getopt() uses). If specified, longopts is a list of strings with the names of the long options which should be supported. The leading '--' characters should not be included in the option name. Options which require an argument should be followed by an equal sign ('='). The return value consists of two elements: the first is a list of (option, value) pairs; the second is the list of program arguments left after the option list was stripped (this is a trailing slice of the first argument). Each option-and-value pair returned has the option as its first element, prefixed with a hyphen (e.g., '-x'), and the option argument as its second element, or an empty string if the option has no argument. The options occur in the list in the same order in which they were found, thus allowing multiple occurrences. Long and short options may be mixed. """ opts = [] if type(longopts) == type(""): longopts = [longopts] else: longopts = list(longopts) if args and args[0].startswith('-') and args[0] != '-': if args[0] == '--': args = args[1:] if args[0].startswith('--'): opts, args = getopt.do_longs(opts, args[0][2:], longopts, args[1:]) else: opts, args = getopt.do_shorts(opts, args[0][1:], shortopts, args[1:]) return opts, args else: return None, args
verify(not has_arg) verify(option == 'abc') has_arg, option = getopt.long_has_args('abc', ['abcd']) verify(not has_arg) verify(option == 'abcd') expectException("has_arg, option = getopt.long_has_args('abc', ['def'])", GetoptError) expectException("has_arg, option = getopt.long_has_args('abc', [])", GetoptError) expectException("has_arg, option = " + \ "getopt.long_has_args('abc', ['abcd','abcde'])", GetoptError) if verbose: print 'Running tests on getopt.do_shorts' opts, args = getopt.do_shorts([], 'a', 'a', []) verify(opts == [('-a', '')]) verify(args == []) opts, args = getopt.do_shorts([], 'a1', 'a:', []) verify(opts == [('-a', '1')]) verify(args == []) #opts, args = getopt.do_shorts([], 'a=1', 'a:', []) #verify(opts == [('-a', '1')]) #verify(args == []) opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) verify(opts == [('-a', '1')]) verify(args == []) opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) verify(opts == [('-a', '1')]) verify(args == ['2']) expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])",
# test_getopt.py # David Goodger <*****@*****.**> 2000-08-19 import getopt from getopt import GetoptError from test_support import verify, verbose def expectException(teststr, expected, failure=AssertionError): """Executes a statement passed in teststr, and raises an exception (failure) if the expected exception is *not* raised.""" try: exec teststr except expected: pass else: raise failure if verbose: print 'Running tests on getopt.short_has_arg' verify(getopt.short_has_arg('a', 'a:')) verify(not getopt.short_has_arg('a', 'a')) expectException("tmp = getopt.short_has_arg('a', 'b')", GetoptError) expectException("tmp = getopt.short_has_arg('a', '')", GetoptError) if verbose: print 'Running tests on getopt.long_has_args' has_arg, option = getopt.long_has_args('abc', ['abc=']) verify(has_arg) verify(option == 'abc') has_arg, option = getopt.long_has_args('abc', ['abc']) verify(not has_arg) verify(option == 'abc') has_arg, option = getopt.long_has_args('abc', ['abcd']) verify(not has_arg)
def update_event(self, inp=-1): self.set_output_val( 0, getopt.do_shorts(self.input(0), self.input(1), self.input(2), self.input(3)))