Using Argparse with the Google Admin API

I am using the Google Python API to output audit information, but I am unable to get the parent group arguments for argparse (which appear to be required to access the API) and my own arguments (like passing a date) to work together.


import pprint
import sys
import re
import httplib2
import json
import collections
import argparse

from oauth2client import client
from apiclient import sample_tools
from apiclient import discovery
from oauth2client.client import AccessTokenRefreshError
from oauth2client.client import OAuth2WebServerFlow
from oauth2client.file import Storage
from import run
from oauth2client import tools

def main(argv): 
  # Parser for command-line arguments.
  parser = argparse.ArgumentParser(

  parser.add_argument("-d","--selected_date", help="Date (YYYY-mm-dd) to run user usage report", required=True)

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

  # Authenticate and construct service.
  service, flags = sample_tools.init(
  argv, 'admin', 'reports_v1', __doc__, __file__,

  # If the Credentials don't exist or are invalid run through the native client
  # flow. The Storage object will ensure that if successful the good
  # Credentials will get written back to a file.
  storage = Storage('admin.dat')
  credentials = storage.get()
  if not credentials or credentials.invalid:
    credentials = run(FLOW, storage)


And by running it from the command line ...

> python
usage: [-h] [--auth_host_name AUTH_HOST_NAME]
                  [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
                  [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}] -d
                  SELECTED_DATE error: argument -d/--selected_date is required


Looks good so far ... now add an argument

> python -d "2014-09-14"
Namespace(auth_host_name='localhost', auth_host_port=[8080, 8090], logging_level='ERROR', noauth_local_webserver=False, selected_date='2014-09-14')
usage: [-h] [--auth_host_name AUTH_HOST_NAME]
                  [--auth_host_port [AUTH_HOST_PORT [AUTH_HOST_PORT ...]]]
                  [--logging_level {DEBUG,INFO,WARNING,ERROR,CRITICAL}] error: unrecognized arguments: -d 2014-09-14


It looks like the date argument is not recognized. Any help would be much appreciated!


source to share

2 answers

Now I'm working - sample_tools.init creates (for better or worse) its own argparse instance. The google API allows passing to parents (where I passed my own arguments) and everything works.

# Parser for command-line arguments
parent = 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='', parents=[parent]) 


Optional argument sample_tools.init (passing parent) fixes the problem



It seems to me that the following is happening:

args = parser.parse_args(argv[1:])   # runs fine
print 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 guess it tools.argparser

does sample_tools.init

and throws an error because it doesn't know about the argument -d


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



All Articles