def _parse_args(self):
"""Setup parser and parse cli arguments.
NB! counter-intuitively, this function also messes around with logging levels.
"""
# We want some of our options to take effect as early as possible, as they affect command line parsing.
# For these options we resort to some ugly, basic argv spotting
if '--debug' in sys.argv:
logging.getLogger().setLevel(logging.DEBUG)
logging.debug('Early debug enabled')
if '--verbose' in sys.argv or '-v' in sys.argv:
logging.getLogger().setLevel(logging.INFO)
autodiscover = False
if '--autodiscover' in sys.argv:
logging.debug('Autodiscover enabled')
autodiscover = True
parser = SmartCommandMapParser(prog=self.tool_shortname,
description="Cligraphy command line tools",
formatter_class=CustomDescriptionFormatter)
self.parser = parser # expose to eg. ctx
parser.add_argument('--version', action=_VersionAction, nargs=0, dest="_version")
parser.add_argument("--debug", help="enable debuging output", dest="_level", action="store_const", const=logging.DEBUG)
parser.add_argument("--pdb", help="run pdb on exceptions", dest="_pdb", action="store_true")
parser.add_argument("--no-capture", help="disable input/output capture", dest="_capture", action="store_false", default=True)
parser.add_argument("--no-reporting", help="disable reporting", dest="_reporting", action="store_false", default=True)
parser.add_argument("--profile", help="enable profiling", dest="_profile", action="store_true", default=False)
parser.add_argument("--autodiscover", help="re-discover commands and refresh cache (default: read cached commands list)", dest="_autodiscover", action="store_true")
parser.add_argument("-v", "--verbose", help="enable informational output", dest="_level", action="store_const", const=logging.INFO)
for namespace, command_map in self.get_command_maps(autodiscover):
parser.add_command_map(namespace, command_map)
argcomplete.autocomplete(parser)
_warn_about_bad_non_ascii_chars(sys.argv)
_warn_about_bad_path(os.getenv('VIRTUAL_ENV'), os.getenv('PATH'))
args = parser.parse_args()
args._parser = parser # deprecated
# pylint:disable=protected-access
if args._level is not None:
logging.getLogger().setLevel(args._level)
return args
评论列表
文章目录