def handle_args_and_set_context(args):
  """
  Args:
    args: the command line args, probably passed from main() as sys.argv[1:]
  Returns:
    a populated EFVersionContext object
  """
  parser = argparse.ArgumentParser()
  parser.add_argument("service_name", help="name of the service")
  parser.add_argument("key", help="version key to look up for <service_name> such as 'ami-id' (list in EF_Config)")
  parser.add_argument("env", help=", ".join(EFConfig.ENV_LIST))
  group = parser.add_mutually_exclusive_group(required=True)
  group.add_argument("--get", help="get current version", action="store_true")
  group.add_argument("--set", help="set current version of <key> to <value> for <service_name>")
  group.add_argument("--rollback", help="set current version to most recent 'stable' version in history",
                     action="store_true")
  group.add_argument("--rollback-to", help="rollback current version to <ami-id> in history",
                     action="store", metavar='<ami-id>')
  group.add_argument("--history", help="Show version history for env/service/key", choices=['json', 'text'])
  group.add_argument("--show", help="Show keys and values. '*' allowed for <key> and <env>",
                     action="store_true", default=False)
  parser.add_argument("--build",
                      help="On --set, also set the externally defined build number associated with the version entity",
                      default="")
  parser.add_argument("--commit_hash", help="On --set, also set the commit hash associated with the version entity",
                      default="")
  parser.add_argument("--commit", help="Actually --set or --rollback (dry run if omitted)",
                      action="store_true", default=False)
  parser.add_argument("--devel", help="Allow running from branch; don't refresh from origin", action="store_true",
                      default=False)
  parser.add_argument("--force_env_full", help="Override env with env_full for account-scoped environments",
                      action="store_true", default=False)
  parser.add_argument("--limit", help="Limit 'history', 'rollback', 'show' to first N records (default 100, max 1000)",
                      type=int, default=100)
  parser.add_argument("--location", help="On --set, also mark the url location of the static build's version file to"
                      "support dist-hash precheck", default="")
  if EFConfig.ALLOW_EF_VERSION_SKIP_PRECHECK:
    parser.add_argument("--noprecheck", help="--set or --rollback without precheck", action="store_true", default=False)
  parser.add_argument("--sr", help="optional /path/to/service_registry_file.json", default=None)
  parser.add_argument("--stable", help="On --set, also mark the version 'stable'", action="store_true")
  parser.add_argument("--verbose", help="Print additional info", action="store_true", default=False)
  # parse
  parsed_args = vars(parser.parse_args(args))
  context = EFVersionContext()
  # marshall the inherited context values
  context._build_number = parsed_args["build"]
  context._commit_hash = parsed_args["commit_hash"]
  context.commit = parsed_args["commit"]
  context.devel = parsed_args["devel"]
  context._force_env_full = parsed_args["force_env_full"]
  try:
    context.env = parsed_args["env"]
  except ValueError as e:
    fail("Error in env: {}".format(e.message))
  # marshall this module's additional context values
  context._get = parsed_args["get"]
  context._history = parsed_args["history"]
  context._key = parsed_args["key"]
  if EFConfig.ALLOW_EF_VERSION_SKIP_PRECHECK:
    context._noprecheck = parsed_args["noprecheck"]
  if not 1 <= parsed_args["limit"] <= 1000:
    fail("Error in --limit. Valid range: 1..1000")
  context._limit = parsed_args["limit"]
  context._location = parsed_args["location"]
  context._rollback = parsed_args["rollback"]
  context._rollback_to = parsed_args["rollback_to"]
  context._service_name = parsed_args["service_name"]
  context._show = parsed_args["show"]
  context._stable = parsed_args["stable"]
  context._value = parsed_args["set"]
  # Set up service registry and policy template path which depends on it
  context.service_registry = EFServiceRegistry(parsed_args["sr"])

  # VERBOSE is global
  global VERBOSE
  VERBOSE = parsed_args["verbose"]

  validate_context(context)
  return context
def handle_args_and_set_context ( args ) : parser = argparse . ArgumentParser ( ) parser . add_argument ( "service_name" , help = "name of the service" ) parser . add_argument ( "key" , help = "version key to look up for <service_name> such as 'ami-id' (list in EF_Config)" ) parser . add_argument ( "env" , help = ", " . join ( EFConfig . ENV_LIST ) ) group = parser . add_mutually_exclusive_group ( required = True ) group . add_argument ( "--get" , help = "get current version" , action = "store_true" ) group . add_argument ( "--set" , help = "set current version of <key> to <value> for <service_name>" ) group . add_argument ( "--rollback" , help = "set current version to most recent 'stable' version in history" , action = "store_true" ) group . add_argument ( "--rollback-to" , help = "rollback current version to <ami-id> in history" , action = "store" , metavar = '<ami-id>' ) group . add_argument ( "--history" , help = "Show version history for env/service/key" , choices = [ 'json' , 'text' ] ) group . add_argument ( "--show" , help = "Show keys and values. '*' allowed for <key> and <env>" , action = "store_true" , default = False ) parser . add_argument ( "--build" , help = "On --set, also set the externally defined build number associated with the version entity" , default = "" ) parser . add_argument ( "--commit_hash" , help = "On --set, also set the commit hash associated with the version entity" , default = "" ) parser . add_argument ( "--commit" , help = "Actually --set or --rollback (dry run if omitted)" , action = "store_true" , default = False ) parser . add_argument ( "--devel" , help = "Allow running from branch; don't refresh from origin" , action = "store_true" , default = False ) parser . add_argument ( "--force_env_full" , help = "Override env with env_full for account-scoped environments" , action = "store_true" , default = False ) parser . add_argument ( "--limit" , help = "Limit 'history', 'rollback', 'show' to first N records (default 100, max 1000)" , type = int , default = 100 ) parser . add_argument ( "--location" , help = "On --set, also mark the url location of the static build's version file to" "support dist-hash precheck" , default = "" ) if EFConfig . ALLOW_EF_VERSION_SKIP_PRECHECK : parser . add_argument ( "--noprecheck" , help = "--set or --rollback without precheck" , action = "store_true" , default = False ) parser . add_argument ( "--sr" , help = "optional /path/to/service_registry_file.json" , default = None ) parser . add_argument ( "--stable" , help = "On --set, also mark the version 'stable'" , action = "store_true" ) parser . add_argument ( "--verbose" , help = "Print additional info" , action = "store_true" , default = False ) parsed_args = vars ( parser . parse_args ( args ) ) context = EFVersionContext ( ) context . _build_number = parsed_args [ "build" ] context . _commit_hash = parsed_args [ "commit_hash" ] context . commit = parsed_args [ "commit" ] context . devel = parsed_args [ "devel" ] context . _force_env_full = parsed_args [ "force_env_full" ] try : context . env = parsed_args [ "env" ] except ValueError as e : fail ( "Error in env: {}" . format ( e . message ) ) context . _get = parsed_args [ "get" ] context . _history = parsed_args [ "history" ] context . _key = parsed_args [ "key" ] if EFConfig . ALLOW_EF_VERSION_SKIP_PRECHECK : context . _noprecheck = parsed_args [ "noprecheck" ] if not 1 <= parsed_args [ "limit" ] <= 1000 : fail ( "Error in --limit. Valid range: 1..1000" ) context . _limit = parsed_args [ "limit" ] context . _location = parsed_args [ "location" ] context . _rollback = parsed_args [ "rollback" ] context . _rollback_to = parsed_args [ "rollback_to" ] context . _service_name = parsed_args [ "service_name" ] context . _show = parsed_args [ "show" ] context . _stable = parsed_args [ "stable" ] context . _value = parsed_args [ "set" ] context . service_registry = EFServiceRegistry ( parsed_args [ "sr" ] ) global VERBOSE VERBOSE = parsed_args [ "verbose" ] validate_context ( context ) return context
Args:
    args: the command line args, probably passed from main() as sys.argv[1:]
  Returns:
    a populated EFVersionContext object