def MakeMutexFooBar(name, help_text):
    """A mutex group arg with two attributes."""
    foo = concepts.SimpleArg(name='foo', key='foo', help_text='A foo')
    bar = concepts.SimpleArg(name='bar', key='bar', help_text='A bar')

    foobar = concepts.GroupArg(name, help_text=help_text, mutex=True)
    foobar.AddConcept(foo)
    foobar.AddConcept(bar)
    return foobar
  def MakeModalFooBar(name, help_text):
    """A modal group arg with two required and one optional attributes."""
    foo = concepts.SimpleArg(name='foo', required=True, key='foo',
                             help_text='A foo')
    bar = concepts.SimpleArg(name='bar', key='bar', help_text='A bar')
    baz = concepts.SimpleArg(name='baz', key='bar', required=True,
                             help_text='A bar')

    foobar = concepts.GroupArg(name, help_text=help_text)
    foobar.AddConcept(foo)
    foobar.AddConcept(bar)
    foobar.AddConcept(baz)
    return foobar
def MakeFooBar(name, help_text, prefixes=False):
    """A group arg with two attributes."""
    foo = concepts.SimpleArg(name='foo',
                             key='foo',
                             help_text='A foo',
                             fallthroughs=[
                                 deps_lib.PropertyFallthrough(
                                     properties.VALUES.core.project)
                             ])
    bar = concepts.SimpleArg(name='bar', key='bar', help_text='A bar')

    foobar = concepts.GroupArg(name, prefixes=prefixes, help_text=help_text)
    foobar.AddConcept(foo)
    foobar.AddConcept(bar)
    return foobar
  def testParseOptionalMutexGroup(self):
    foo = concepts.SimpleArg(name='foo', key='foo', help_text='A foo')
    bar = concepts.SimpleArg(name='bar', key='bar', help_text='A bar')
    foobar = concepts.GroupArg('foo-bar', help_text='help 1', mutex=True)
    foobar.AddConcept(foo)
    foobar.AddConcept(bar)

    manager = concept_managers.ConceptManager()
    manager.AddConcept(foobar)
    manager.AddToParser(self.parser)
    with self.assertRaisesRegex(
        cli_test_base.MockArgumentError,
        'argument --bar: At most one of --bar | --foo may be specified.'):
      args = self.parser.parser.parse_args(['--foo', 'x', '--bar', 'y'])
      args.CONCEPT_ARGS.ParseConcepts()
  def testParseRequiredMutexGroup(self):
    foo = concepts.SimpleArg(name='foo', key='foo', help_text='A foo')
    bar = concepts.SimpleArg(name='bar', key='bar', help_text='A bar')
    foobar = concepts.GroupArg('foo-bar', required=True, help_text='help 1',
                               mutex=True)
    foobar.AddConcept(foo)
    foobar.AddConcept(bar)

    manager = concept_managers.ConceptManager()
    manager.AddConcept(foobar)
    manager.AddToParser(self.parser)
    with self.assertRaisesRegex(
        cli_test_base.MockArgumentError,
        re.escape(
            'argument --bar: Exactly one of (--bar | --foo) must be specified.'
        )):
      args = self.parser.parser.parse_args(['--foo', 'x', '--bar', 'y'])
      args.CONCEPT_ARGS.ParseConcepts()
 def testParseRequiredMissing(self):
   foo = concepts.SimpleArg(name='foo', required=True, key='foo',
                            help_text='A foo')
   manager = concept_managers.ConceptManager()
   manager.AddConcept(foo)
   manager.AddToParser(self.parser)
   with self.assertRaisesRegex(
       cli_test_base.MockArgumentError,
       'argument --foo: Must be specified'):
     args = self.parser.parser.parse_args()
     args.CONCEPT_ARGS.ParseConcepts()
  def testParseOptionalMutexGroupFallthrough(self):
    foo = concepts.SimpleArg(name='foo', key='foo', help_text='A foo',
                             fallthroughs=[
                                 deps_lib.Fallthrough(lambda: 4, hint='none'),
                             ])
    bar = concepts.SimpleArg(name='bar', key='bar', help_text='A bar',
                             fallthroughs=[
                                 deps_lib.Fallthrough(lambda: 4, hint='none'),
                             ])
    foobar = concepts.GroupArg('foo-bar', help_text='help 1', mutex=True)
    foobar.AddConcept(foo)
    foobar.AddConcept(bar)

    manager = concept_managers.ConceptManager()
    manager.AddConcept(foobar)
    manager.AddToParser(self.parser)
    with self.assertRaisesRegex(
        exceptions.OptionalMutexGroupError,
        re.escape('Failed to specify [foo-bar]: '
                  'At most one of --foo | --bar may be specified.')):
      args = self.parser.parser.parse_args()
      args.CONCEPT_ARGS.ParseConcepts()
 def testParseRequiredMissingWithFallback(self):
   foo = concepts.SimpleArg(name='foo', required=True, key='foo',
                            help_text='A foo',
                            fallthroughs=[deps_lib.ArgFallthrough('notfound')])
   manager = concept_managers.ConceptManager()
   manager.AddConcept(foo)
   manager.AddToParser(self.parser)
   with self.assertRaisesRegex(
       exceptions.MissingRequiredArgumentError, re.escape(
           'No value was provided for [--foo]: Failed to find attribute. '
           'The attribute can be set in the following ways: \n'
           '- provide the argument [--foo] on the command line\n'
           '- provide the argument [notfound] on the command line')):
     args = self.parser.parser.parse_args()
     args.CONCEPT_ARGS.ParseConcepts()
  def MakeModalFooBarWithInvalidFallthrough(name, help_text):
    """A modal group arg with an invalid fallthrough.

    Holds a required and an optional attribute.

    Args:
      name: str, name of the modal group.
      help_text: str, help text for the modal group.

    Returns:
      the modal group.
    """
    foo = concepts.SimpleArg(
        name='foo', required=True, key='foo',
        help_text='A foo',
        fallthroughs=[
            deps_lib.ArgFallthrough('notfound')
        ])
    bar = concepts.SimpleArg(name='bar', key='bar', help_text='A bar')

    foobar = concepts.GroupArg(name, help_text=help_text)
    foobar.AddConcept(foo)
    foobar.AddConcept(bar)
    return foobar
Exemple #10
0
 def Args(parser):
   manager = concept_managers.ConceptManager()
   manager.AddConcept(concepts.SimpleArg(name='required', help_text='help 1',
                                         required=True))
   manager.AddToParser(parser)
Exemple #11
0
 def Args(parser):
     manager = concept_managers.ConceptManager()
     manager.AddConcept(concepts.SimpleArg(name='a', help_text='help 1'))
     manager.AddToParser(parser)