Skip to content Skip to sidebar Skip to footer

Using Argparse With Google Admin Api

I am using Google's Python API to pull down auditing information, but I can't get the parent group arguments for argparse (which appear to be required for API access) and my own ar

Solution 1:

I have it working now- sample_tools.init is instantiating (for better or worse) its own argparse instance. The Google API allows you to pass in a parent (where I passed in my own custom arguments) and everything is works.

https://google-api-python-client.googlecode.com/hg/docs/epy/apiclient.sample_tools-pysrc.html

# Parser for command-line argumentsparent = argparse.ArgumentParser(add_help=False)
group = parent.add_argument_group('standard')
parent.add_argument("-d","--selected_date", help="Date (YYYY-mm-dd) to run user usage report", required=True)

flags = parser.parse_args(argv[1:])
print flags
selected_date = flags.selected_date
print selected_date

# Authenticate and construct service.
service, flags = sample_tools.init(
  argv, 'admin', 'reports_v1', __doc__, __file__,
  scope='https://www.googleapis.com/auth/admin.reports.usage.readonly', parents=[parent]) 

The extra argument to sample_tools.init (passing the parent) fixes the problem

Solution 2:

It looks to me like the following is happening:

args = parser.parse_args(argv[1:])   # runs fineprint args                           # produces the Namespace line
selected_date = args.selected_date
print selected_date                  # where is this output?# Authenticate and construct service.
service, flags = sample_tools.init(...)  # is this producing the error?

I'm guessing that the tools.argparser is being run by sample_tools.init, and producing the error because it doesn't know about the -d argument.

(I'm familiar with argparse, but not this API).

Post a Comment for "Using Argparse With Google Admin Api"