def macGetArgs(): import EasyDialogs EasyDialogs.Message("""\ Use the next dialog to build a command line: 1. Choose an output format from the [Option] list 2. Click [Add] 3. Choose an input file: [Add existing file...] 4. Save the output: [Add new file...] 5. [OK]""") optionlist = [(longopt, description) for (longopt, shortopt, description) in options] argv = EasyDialogs.GetArgv(optionlist=optionlist, addfolder=0) return posixGetArgs(argv)
def parse_command_line(self): """Parse the setup script's command line, taken from the 'script_args' instance attribute (which defaults to 'sys.argv[1:]' -- see 'setup()' in core.py). This list is first processed for "global options" -- options that set attributes of the Distribution instance. Then, it is alternately scanned for Distutils commands and options for that command. Each new command terminates the options for the previous command. The allowed options for a command are determined by the 'user_options' attribute of the command class -- thus, we have to be able to load command classes in order to parse the command line. Any error in that 'options' attribute raises DistutilsGetoptError; any error on the command-line raises DistutilsArgError. If no Distutils commands were found on the command line, raises DistutilsArgError. Return true if command-line was successfully parsed and we should carry on with executing commands; false if no errors but we shouldn't execute commands (currently, this only happens if user asks for help). """ # # We now have enough information to show the Macintosh dialog # that allows the user to interactively specify the "command line". # if sys.platform == 'mac': import EasyDialogs cmdlist = self.get_command_list() self.script_args = EasyDialogs.GetArgv( self.global_options + self.display_options, cmdlist) # We have to parse the command line a bit at a time -- global # options, then the first command, then its options, and so on -- # because each command will be handled by a different class, and # the options that are valid for a particular class aren't known # until we have loaded the command class, which doesn't happen # until we know what the command is. self.commands = [] parser = FancyGetopt(self.global_options + self.display_options) parser.set_negative_aliases(self.negative_opt) parser.set_aliases({'licence': 'license'}) args = parser.getopt(args=self.script_args, object=self) option_order = parser.get_option_order() # for display options we return immediately if self.handle_display_options(option_order): return while args: args = self._parse_command_opts(parser, args) if args is None: # user asked for help (and got it) return # Handle the cases of --help as a "global" option, ie. # "setup.py --help" and "setup.py --help command ...". For the # former, we show global options (--verbose, --dry-run, etc.) # and display-only options (--name, --version, etc.); for the # latter, we omit the display-only options and show help for # each command listed on the command line. if self.help: self._show_help(parser, display_options=len(self.commands) == 0, commands=self.commands) return # Oops, no commands found -- an end-user error if not self.commands: raise DistutilsArgError, "no commands supplied" # All is well: return true return 1
import os, sys, EasyDialogs, Image # instead of relying on sys.argv, ask the user via a simple dialog: rotater = ('Rotate right', 'Rotate image by 90 degrees clockwise') rotatel = ('Rotate left', 'Rotate image by 90 degrees anti-clockwise') scale = ('Makethumb', 'Make a 100x100 thumbnail') str = ['Format JPG', 'Format PNG'] cmd = [rotater, rotatel, scale] optlist = EasyDialogs.GetArgv(str, cmd, addoldfile=False, addnewfile=False, addfolder=True) # now we can parse the arguments and options (we could use getopt, too): dirs = [] format = "JPEG" rotationr = False rotationl = False resize = False for arg in optlist: if arg == "--Format JPG": format = "JPEG" if arg == "--Format PNG": format = "PNG" if arg == "Rotate right": rotationr = True if arg == "Rotate left": rotationl = True if arg == "Makethumb": resize = True if os.path.isdir(arg): dirs.append(arg)
arguments = EasyDialogs.GetArgv( [ ('c=', 'program passed in as string (terminates option list)'), ('d', 'Debug'), ('E', 'Ignore environment variables'), ('i', 'Inspect interactively after running'), ('m=', 'run library module as a script (terminates option list)'), ('O', 'Optimize generated bytecode'), ('Q=', 'division options: -Qold (default), -Qwarn, -Qwarnall, -Qnew'), ('S', "don't imply 'import site' on initialization"), ('t', 'issue warnings about inconsistent tab usage'), ('tt', 'issue errors about inconsistent tab usage'), ('u', 'unbuffered binary stdout and stderr'), ('v', 'verbose (trace import statements)'), ('V', 'print the Python version number and exit'), ('W=', 'warning control (arg is action:message:category:module:lineno)'), ('x', 'skip first line of source, allowing use of non-Unix forms of #!cmd'), ], commandlist=[ ('python', 'Default Interpreter'), ('python2.5', 'Python 2.5'), ('pyhton2.4', 'Python 2.4'), ], addoldfile=True, addnewfile=False, addfolder=False, ) print arguments
#Starting with some header import EasyDialogs import os import Image import sys # Number one dialog, this one take the parameter, tuples indicate command and description, # First list the option (maybe with parameter if followed by ':' or '='), second list the sum of the command rotater = ('Rotate right', 'Rotate image by 90 degrees clockwise') rotatel = ('Rotate left', 'Rotate image by 90 degrees anti-clockwise') scale = ('Makethumb', 'Make a 100x100 thumbnail') str = ['Format JPG', 'Format PNG'] cmd = [rotater, rotatel, scale] # This dialog take some argument, the two list (argument, and command), and if user can add file or directory optlist = EasyDialogs.GetArgv(str, cmd, addoldfile = 0, addnewfile = 0, addfolder = 1) dir = [] format = "JPEG" rotationr = 0 rotationl = 0 resize = 0 val = 0 # Parsing argument, we can do it also with getopt, but take it easy for arg in optlist: if arg == "--Format JPG": format = "JPEG" if arg == "--Format PNG": format = "PNG" if arg == "Rotate right":