youngking avatar youngking committed 63f1655

change required action's behavor

Comments (0)

Files changed (1)

kahgean/options.py

 class Options(object):
     """the Options class
     """
-    
+
     def __init__(self, prog=None, description=None, epilog=None,
                  argument_default=SUPPRESS, formatter_class=HelpFormatter,
                  config_argument=None, config_file_dest='config_file',
                                           argument_default=argument_default,
                                           formatter_class=formatter_class)
         self.add_option(*self.config_argument, dest=self.config_file_dest,
-                          metavar='filename', type=file, default=SUPPRESS,
-                          help='path to the configuration file')
+                        metavar='filename', type=file, default=SUPPRESS,
+                        help='path to the configuration file')
 
     def add_option(self, *args, **kwargs):
         """add option define
-        
+
         For more information, please read the `argparse`_ document.
-        
+
         .. _argparse:http://docs.python.org/library/argparse.html#\
 the-add-argument-method
         """
         action = self._arg_parser.add_argument(*args, **kwargs)
-    
+
     def parse_options(self, args=None):
         """parse options from command-line and/or configuration file"""
         # preparing...
+        required_actions = []
         for action in self._arg_parser._actions:
             # make bool support 'on', 'off', etc.
             if action.type == bool:
                 action.default = SUPPRESS
             # there is a side effect, we cannot support nargs="+" or N
             # use nargs="*" instead
-            if action.nargs=='+' or (isinstance(action.nargs, int)
-                                     and action.nargs>0):
+            if action.nargs == '+' or (isinstance(action.nargs, int)
+                                       and action.nargs > 0):
                 warnings.warn('not support nargs="+" or N, use "*" instead',
                               Warning)
                 action.nargs = '*'
+
+            # hacking reqired arguments
+            if action.required:
+                action.required = False
+                required_actions.append(action.dest)
+
         # parsing the command-line...
         ns_a = self._arg_parser.parse_args(args)
         if hasattr(ns_a, self.config_file_dest):
         for dest in self._defaults:
             if not hasattr(ns_a, dest):
                 setattr(ns_a, dest, self._defaults[dest])
+
+        # restore required arguments
+        for dest in required_actions:
+            if not hasattr(ns_a, dest):
+                self._arg_parser.error('argument %s is required' % dest)
         self._namespace = ns_a
 
     def _option_to_arg(self, section, option, value):
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.