Skip to content

Commit

Permalink
Refactor conda init shells as argparse choices (conda#11897)
Browse files Browse the repository at this point in the history
  • Loading branch information
kenodegard authored Sep 29, 2022
1 parent c217038 commit bfcbbf6
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 18 deletions.
21 changes: 17 additions & 4 deletions conda/cli/conda_argparse.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

from .. import __version__
from ..auxlib.ish import dals
from ..auxlib.compat import isiterable
from ..base.constants import COMPATIBLE_SHELLS, CONDA_HOMEPAGE_URL, DepsModifier, \
UpdateModifier, ExperimentalSolverChoice
from ..common.constants import NULL
Expand Down Expand Up @@ -173,6 +174,14 @@ def print_help(self):
builder.extend(' %s' % cmd for cmd in sorted(other_commands))
print('\n'.join(builder))

def _check_value(self, action, value):
# extend to properly handle when we accept multiple choices and the default is a list
if action.choices is not None and isiterable(value):
for element in value:
super()._check_value(action, element)
else:
super()._check_value(action, value)


def _exec(executable_args, env_vars):
return (_exec_win if on_win else _exec_unix)(executable_args, env_vars)
Expand Down Expand Up @@ -723,10 +732,14 @@ def configure_parser_init(sub_parsers):
p.add_argument(
'shells',
nargs='*',
help="One or more shells to be initialized. If not given, the default value is "
"'bash' on unix and 'cmd.exe' on Windows. Use the '--all' flag to initialize "
"all shells. Currently compatible shells are {%s}."
% ", ".join(sorted(COMPATIBLE_SHELLS)),
choices=COMPATIBLE_SHELLS,
metavar="SHELLS",
help=(
"One or more shells to be initialized. If not given, the default value is 'bash' on "
"unix and 'cmd.exe' & 'powershell' on Windows. Use the '--all' flag to initialize all "
f"shells. Available shells: {sorted(COMPATIBLE_SHELLS)}"
),
default=["cmd.exe", "powershell"] if on_win else ["bash"],
)

if on_win:
Expand Down
18 changes: 4 additions & 14 deletions conda/cli/main_init.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

from ..base.context import context
from ..common.compat import on_win
from ..exceptions import ArgumentError

log = getLogger(__name__)

Expand All @@ -18,26 +19,15 @@ def execute(args, parser):
if args.install:
return install(context.conda_prefix)

invalid_shells = tuple(s for s in args.shells if s not in COMPATIBLE_SHELLS)
if invalid_shells:
from ..exceptions import ArgumentError
from ..common.io import dashlist
raise ArgumentError("Invalid shells: %s\n\n"
"Currently available shells are:%s"
% (dashlist(invalid_shells), dashlist(sorted(COMPATIBLE_SHELLS))))

if args.all:
selected_shells = COMPATIBLE_SHELLS
else:
selected_shells = tuple(args.shells)

if not selected_shells:
selected_shells = ('cmd.exe', 'powershell') if on_win else ('bash',)

if args.dev:
assert len(selected_shells) == 1, "--dev can only handle one shell at a time right now"
shell = selected_shells[0]
return initialize_dev(shell)
if len(selected_shells) != 1:
raise ArgumentError("--dev can only handle one shell at a time right now")
return initialize_dev(selected_shells[0])

else:
for_user = args.user
Expand Down

0 comments on commit bfcbbf6

Please sign in to comment.