def test_do_longs(self): opts, args = getopt.do_longs([], 'abc', ['abc'], []) self.assertEqual(opts, [('--abc', '')]) self.assertEqual(args, []) opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) self.assertEqual(opts, [('--abc', '1')]) self.assertEqual(args, []) opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) self.assertEqual(opts, [('--abcd', '1')]) self.assertEqual(args, []) opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) self.assertEqual(opts, [('--abc', '')]) self.assertEqual(args, []) # Much like the preceding, except with a non-alpha character ("-") in # option name that precedes "="; failed in # http://python.org/sf/126863 opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], []) self.assertEqual(opts, [('--foo', '42')]) self.assertEqual(args, []) self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], []) self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])
def test_do_longs(self): opts, args = getopt.do_longs([], "abc", ["abc"], []) self.assertEqual(opts, [("--abc", "")]) self.assertEqual(args, []) opts, args = getopt.do_longs([], "abc=1", ["abc="], []) self.assertEqual(opts, [("--abc", "1")]) self.assertEqual(args, []) opts, args = getopt.do_longs([], "abc=1", ["abcd="], []) self.assertEqual(opts, [("--abcd", "1")]) self.assertEqual(args, []) opts, args = getopt.do_longs([], "abc", ["ab", "abc", "abcd"], []) self.assertEqual(opts, [("--abc", "")]) self.assertEqual(args, []) # Much like the preceding, except with a non-alpha character ("-") in # option name that precedes "="; failed in # http://python.org/sf/126863 opts, args = getopt.do_longs([], "foo=42", ["foo-bar", "foo="], []) self.assertEqual(opts, [("--foo", "42")]) self.assertEqual(args, []) self.assertError(getopt.do_longs, [], "abc=1", ["abc"], []) self.assertError(getopt.do_longs, [], "abc", ["abc="], [])
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_longs(self): opts, args = getopt.do_longs([], 'abc', ['abc'], []) self.assertEqual(opts, [('--abc', '')]) self.assertEqual(args, []) opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) self.assertEqual(opts, [('--abc', '1')]) self.assertEqual(args, []) opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) self.assertEqual(opts, [('--abcd', '1')]) self.assertEqual(args, []) opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) self.assertEqual(opts, [('--abc', '')]) self.assertEqual(args, []) opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo='], []) self.assertEqual(opts, [('--foo', '42')]) self.assertEqual(args, []) self.assertError(getopt.do_longs, [], 'abc=1', ['abc'], []) self.assertError(getopt.do_longs, [], 'abc', ['abc='], [])
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(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', [])", GetoptError) expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])", GetoptError) if verbose: print 'Running tests on getopt.do_longs' opts, args = getopt.do_longs([], 'abc', ['abc'], []) verify(opts == [('--abc', '')]) verify(args == []) opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) verify(opts == [('--abc', '1')]) verify(args == []) opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) verify(opts == [('--abcd', '1')]) verify(args == []) opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) verify(opts == [('--abc', '')]) verify(args == []) # Much like the preceding, except with a non-alpha character ("-") in # option name that precedes "="; failed in # http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470 opts, args = getopt.do_longs([], 'foo=42', ['foo-bar', 'foo=',], [])
# 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_longs(self.input(0), self.input(1), self.input(2), self.input(3)))
#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', [])", GetoptError) expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])", GetoptError) if verbose: print 'Running tests on getopt.do_longs' opts, args = getopt.do_longs([], 'abc', ['abc'], []) verify(opts == [('--abc', '')]) verify(args == []) opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) verify(opts == [('--abc', '1')]) verify(args == []) opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) verify(opts == [('--abcd', '1')]) verify(args == []) opts, args = getopt.do_longs([], 'abc', ['ab', 'abc', 'abcd'], []) verify(opts == [('--abc', '')]) verify(args == []) # Much like the preceding, except with a non-alpha character ("-") in # option name that precedes "="; failed in # http://sourceforge.net/bugs/?func=detailbug&bug_id=126863&group_id=5470 opts, args = getopt.do_longs([], 'foo=42', [
#assert opts == [('-a', '1')] #assert args == [] opts, args = getopt.do_shorts([], 'a', 'a:', ['1']) assert opts == [('-a', '1')] assert args == [] opts, args = getopt.do_shorts([], 'a', 'a:', ['1', '2']) assert opts == [('-a', '1')] assert args == ['2'] expectException("opts, args = getopt.do_shorts([], 'a1', 'a', [])", GetoptError) expectException("opts, args = getopt.do_shorts([], 'a', 'a:', [])", GetoptError) if verbose: print 'Running tests on getopt.do_longs' opts, args = getopt.do_longs([], 'abc', ['abc'], []) assert opts == [('--abc', '')] assert args == [] opts, args = getopt.do_longs([], 'abc=1', ['abc='], []) assert opts == [('--abc', '1')] assert args == [] opts, args = getopt.do_longs([], 'abc=1', ['abcd='], []) assert opts == [('--abcd', '1')] assert args == [] expectException("opts, args = getopt.do_longs([], 'abc=1', ['abc'], [])", GetoptError) expectException("opts, args = getopt.do_longs([], 'abc', ['abc='], [])", GetoptError) # note: the empty string between '-a' and '--beta' is significant: # it simulates an empty string option argument ('-a ""') on the command line.