Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Empty file modified az
100755 → 100644
Empty file.
48 changes: 26 additions & 22 deletions azure-cli.pyproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,8 @@
<OutputPath>.</OutputPath>
<ProjectTypeGuids>{888888a0-9f3d-457c-b088-3a5042f75d52}</ProjectTypeGuids>
<LaunchProvider>Standard Python launcher</LaunchProvider>
<InterpreterId>{1dd9c42b-5980-42ce-a2c3-46d3bf0eede4}</InterpreterId>
<InterpreterId>{4ae3497d-f45c-4ec2-9e26-57476ef276a0}</InterpreterId>
<InterpreterVersion>3.5</InterpreterVersion>
<CommandLineArguments>
</CommandLineArguments>
<EnableNativeCodeDebugging>False</EnableNativeCodeDebugging>
</PropertyGroup>
<PropertyGroup Condition="'$(Configuration)' == 'Debug'" />
<PropertyGroup Condition="'$(Configuration)' == 'Release'" />
Expand All @@ -28,34 +25,19 @@
<Compile Include="azure\cli\commands\account.py" />
<Compile Include="azure\cli\commands\login.py" />
<Compile Include="azure\cli\commands\logout.py" />
<Compile Include="azure\cli\commands\network.py" />
<Compile Include="azure\cli\commands\resourcegroup.py" />
<Compile Include="azure\cli\commands\storage.py" />
<Compile Include="azure\cli\commands\vm.py" />
<Compile Include="azure\cli\commands\_auto_command.py" />
<Compile Include="azure\cli\commands\__init__.py" />
<Compile Include="azure\cli\main.py" />
<Compile Include="azure\cli\tests\test_argparse.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="azure\cli\tests\test_autocommand.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="azure\cli\tests\test_connection_verify.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="azure\cli\tests\test_autocommand.py" />
<Compile Include="azure\cli\tests\test_connection_verify.py" />
<Compile Include="azure\cli\tests\test_output.py" />
<Compile Include="azure\cli\_locale.py" />
<Compile Include="azure\cli\tests\test_profile.py" />
<Compile Include="azure\cli\_argparse.py" />
<Compile Include="azure\cli\commands\_command_creation.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="azure\cli\_debug.py">
<SubType>Code</SubType>
</Compile>
<Compile Include="azure\cli\_locale.py" />
<Compile Include="azure\cli\_logging.py" />
<Compile Include="azure\cli\_output.py" />
<Compile Include="azure\cli\_profile.py" />
<Compile Include="azure\cli\_session.py">
<SubType>Code</SubType>
Expand All @@ -72,6 +54,28 @@
<Folder Include="azure\cli\tests\" />
</ItemGroup>
<ItemGroup>
<Interpreter Include="..\..\Temp\burt\env27b\">
<Id>{83c20e12-84e3-4c10-a1ba-466d2b57483d}</Id>
<BaseInterpreter>{2af0f10d-7135-4994-9156-5d01c9c11b7e}</BaseInterpreter>
<Version>2.7</Version>
<Description>env27b (Python 2.7)</Description>
<InterpreterPath>Scripts\python.exe</InterpreterPath>
<WindowsInterpreterPath>Scripts\pythonw.exe</WindowsInterpreterPath>
<LibraryPath>Lib\</LibraryPath>
<PathEnvironmentVariable>PYTHONPATH</PathEnvironmentVariable>
<Architecture>X86</Architecture>
</Interpreter>
<Interpreter Include="..\..\Temp\burt\env35d\">
<Id>{4ae3497d-f45c-4ec2-9e26-57476ef276a0}</Id>
<BaseInterpreter>{2af0f10d-7135-4994-9156-5d01c9c11b7e}</BaseInterpreter>
<Version>3.5</Version>
<Description>env35d (Python 3.5)</Description>
<InterpreterPath>Scripts\python.exe</InterpreterPath>
<WindowsInterpreterPath>Scripts\pythonw.exe</WindowsInterpreterPath>
<LibraryPath>Lib\</LibraryPath>
<PathEnvironmentVariable>PYTHONPATH</PathEnvironmentVariable>
<Architecture>X86</Architecture>
</Interpreter>
<Interpreter Include="..\env\">
<Id>{1dd9c42b-5980-42ce-a2c3-46d3bf0eede4}</Id>
<BaseInterpreter>{2af0f10d-7135-4994-9156-5d01c9c11b7e}</BaseInterpreter>
Expand Down
27 changes: 19 additions & 8 deletions src/azure/cli/_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,10 @@ def add_command(self,
m['$args'] = []
m['$kwargs'] = kw = {}
m['$argdoc'] = ad = []
for spec, desc in args or []:
for spec, desc, req in args or []:
if not any(spec.startswith(p) for p in ARG_PREFIXES):
m['$args'].append(spec.strip('<> '))
ad.append((spec, desc))
ad.append((spec, desc, req))
continue

aliases = spec.split()
Expand All @@ -119,10 +119,12 @@ def add_command(self,
else:
v = aliases.pop().strip('<> ')
target, _ = _read_arg(aliases[0])
kw.update({_read_arg(a)[0]: (target, v) for a in aliases})
ad.append(('/'.join(aliases), desc))

kw.update({_read_arg(a)[0]: (target, v, req) for a in aliases})
ad.append(('/'.join(aliases), desc, req))

#pylint: disable=too-many-branches
#pylint: disable=too-many-statements
#pylint: disable=too-many-locals
def execute(self,
args,
show_usage=False,
Expand Down Expand Up @@ -209,6 +211,14 @@ def not_global(a):
parsed.positional.append(n)
n = next_n

required_args = [x for x, _, req in expected_kwargs.values() if req]
for a in required_args:
try:
parsed[a]
except KeyError:
print(L("Missing required argument {}".format(a)))
return self._display_usage(nouns, m, out)

old_stdout = sys.stdout
try:
sys.stdout = out
Expand Down Expand Up @@ -236,9 +246,10 @@ def _display_usage(self, nouns, noun_map, out=sys.stdout):
argdoc = noun_map.get('$argdoc')
if argdoc:
print('Arguments', file=out)
maxlen = max(len(a) for a, d in argdoc)
for a, d in argdoc:
print(' {0:<{1}} - {2}'.format(a, maxlen, d), file=out)
maxlen = max(len(a) for a, d, r in argdoc)
for a, d, r in argdoc:
print(' {0:<{1}} - {2} {3}'.format(a, maxlen, d, L("[Required]") if r else ""),
file=out)
print(file=out)
out.flush()

Expand Down
5 changes: 3 additions & 2 deletions src/azure/cli/commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,10 @@ def add_description(handler):
return handler
return add_description

def option(spec, description_text=None):
def option(spec, description_text=None, required=False):
def add_option(handler):
_COMMANDS.setdefault(handler, {}).setdefault('args', []).append((spec, description_text))
_COMMANDS.setdefault(handler, {}).setdefault('args', []) \
.append((spec, description_text, required))
logger.debug('Added option "%s" to %s', spec, handler)
return handler
return add_option
Expand Down
15 changes: 13 additions & 2 deletions src/azure/cli/tests/test_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ def set_n3(a, b): res[2] = True

def test_args(self):
p = ArgumentParser('test')
p.add_command(lambda a, b: (a, b), 'n1', args=[('--arg -a', ''), ('-b <v>', '')])
p.add_command(lambda a, b: (a, b), 'n1', args=[('--arg -a', '', False), ('-b <v>', '', False)])

res, other = p.execute('n1 -a x'.split())
self.assertTrue(res.arg)
Expand Down Expand Up @@ -69,7 +69,7 @@ def test_args(self):

def test_unexpected_args(self):
p = ArgumentParser('test')
p.add_command(lambda a, b: (a, b), 'n1', args=[('-a', '')])
p.add_command(lambda a, b: (a, b), 'n1', args=[('-a', '', False)])

res, other = p.execute('n1 -b=2'.split())
self.assertFalse(res)
Expand All @@ -84,5 +84,16 @@ def test_unexpected_args(self):
self.assertEqual('2', other.b.c.d)
self.assertEqual('3', other.b.c.e)

def test_required_args(self):
p = ArgumentParser('test')
p.add_command(lambda a, b: (a, b), 'n1', args=[('--arg -a', '', True), ('-b <v>', '', False)])

res, other = p.execute('n1 -a x'.split())
self.assertTrue(res.arg)
self.assertSequenceEqual(res.positional, ['x'])

self.assertIsNone(p.execute('n1 -b x'.split()))


if __name__ == '__main__':
unittest.main()
2 changes: 1 addition & 1 deletion src/azure/cli/tests/test_autocommand.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def testfunc():
self.assertIsNotNone(registered_command)
self.assertEqual(registered_command['name'], command_name)
self.assertEqual(len(registered_command['args']), 1)
self.assertEqual(registered_command['args'][0], (spec, desc))
self.assertEqual(registered_command['args'][0], (spec, desc, False))

def test_load_test_commands(self):
import sys
Expand Down