def main(unused_argv):
  if not FLAGS.discovery:
    raise app.UsageError('You must specify --discovery')
  if not (FLAGS.output_dir or FLAGS.output_file):
    raise app.UsageError(
        'You must specify one of --output_dir or --output_file')
  if not FLAGS.templates:
    raise app.UsageError('You must specify --templates')

  f = open(FLAGS.discovery)
  discovery_doc = json.loads(f.read())
  f.close()

  options = {
      # Include other files needed to compile (e.g. base jar files)
      'include_dependencies': False,
      # Include the timestamp in the generated library
      'include_timestamp': FLAGS.include_timestamp,
      # Put API version in the package
      'version_package': FLAGS.version_package,
      }
  if FLAGS.output_type == 'full':
    options['include_dependencies'] = True

  # try to determine what generator to use from the language variant.
  language_variants = Targets().VariationsForLanguage(FLAGS.language)
  if language_variants:
    features = language_variants.GetFeatures(FLAGS.language_variant)
    template_dir = features.template_dir
    generator_name = features.get('generator', FLAGS.language)
  else:
    # Not described by targets.json. The template dir should just be down
    # in the language specific folder
    template_dir = os.path.join(os.path.dirname(__file__), 'languages',
                                FLAGS.language)
    features = None
    generator_name = FLAGS.language

  # Instantiate the right code generator
  lang_model = None
  try:
    if FLAGS.language == 'any':
      api = Api(discovery_doc)
      # TODO(user): A default language model should be built in to the
      #   templates
      lang_model = language_model.DocumentingLanguageModel()
    else:
      generator_class = generator_lookup.GetGeneratorByLanguage(generator_name)
      generator_instance = generator_class(discovery_doc)
      api = generator_instance.api
      lang_model = generator_instance.language_model

  except __HOLE__:
    raise app.UsageError('Unsupported language option: %s' % FLAGS.language)

  api.VisitAll(lambda o: o.SetLanguageModel(lang_model))
  gen = TemplateExpander(api, options=options)

  if features:
    gen.SetFeatures(features)

  template_dir = os.path.join(template_dir, FLAGS.templates)
  if not os.path.isdir(template_dir):
    raise app.UsageError('Can not find template tree at: %s' % template_dir)
  gen.SetTemplateDir(template_dir)

  # Get an output writer
  package_writer = package_writer_foundry.GetPackageWriter(
      output_dir=FLAGS.output_dir, output_file=FLAGS.output_file,
      output_format=FLAGS.output_format)

  # do it
  gen.GeneratePackage(package_writer)
  package_writer.DoneWritingArchive()
  return 0
dataset/ETHPy150Open google/apis-client-generator/src/googleapis/codegen/expand_templates.py/main