Skip to content

Commit

Permalink
Put a nail in aliases / Move contrib.help to own file.
Browse files Browse the repository at this point in the history
Aliases were never properly conceived.
  • Loading branch information
mayfield committed Apr 9, 2017
1 parent ff1e607 commit bf34216
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 88 deletions.
2 changes: 1 addition & 1 deletion shellish/command/contrib/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from .syscomplete import *
from .interactive import *
from .ini import *
from .alias import *
from .tree import *
from .commands import *
from .help import *
25 changes: 0 additions & 25 deletions shellish/command/contrib/alias.py

This file was deleted.

43 changes: 43 additions & 0 deletions shellish/command/contrib/help.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
"""
Alternate form of getting help.
"""

from .. import command


class Help(command.Command):
""" Show help for a command. """

name = 'help'
use_pager = True

def setup_args(self, parser):
self.add_argument('command', nargs='?', complete=self.command_choices,
help='Command name to show help for.')

def command_choices(self, prefix, args):
return frozenset(x for x in self.parent.subcommands
if x.startswith(prefix))

def run(self, args):
if not args.command:
self.print_overview()
else:
try:
command = self.parent.subcommands[args.command]
except KeyError:
raise SystemExit("Invalid command")
command.argparser.print_help()

def print_overview(self):
ap = self.find_root().argparser
formatter = ap._get_formatter()
formatter.add_text(ap.description)
for x in ap._action_groups:
if x.title == 'subcommands':
formatter.start_section(x.title)
formatter.add_text(x.description)
formatter.add_arguments(x._group_actions)
formatter.end_section()
break
print(formatter.format_help())
44 changes: 0 additions & 44 deletions shellish/command/contrib/interactive.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,50 +3,6 @@
"""

from .. import command
from ... import rendering


class Help(command.Command):
""" Show help for a command. """

name = 'help'
use_pager = True

def setup_args(self, parser):
self.add_argument('command', nargs='?', complete=self.command_choices,
help='Command name to show help for.')

def command_choices(self, prefix, args):
return frozenset(x for x in self.parent.subcommands
if x.startswith(prefix))

def run(self, args):
if not args.command:
self.print_overview()
else:
try:
command = self.parent.subcommands[args.command]
except KeyError:
raise SystemExit("Invalid command")
command.argparser.print_help()

def print_overview(self):
ap = self.find_root().argparser
formatter = ap._get_formatter()
formatter.add_text(ap.description)
for x in ap._action_groups:
if x.title == 'subcommands':
formatter.start_section(x.title)
formatter.add_text(x.description)
formatter.add_arguments(x._group_actions)
formatter.end_section()
break
print(formatter.format_help())
if self.session.aliases:
print(' ALIAS')
for k, v in self.session.aliases.items():
print(' %-13s%s %s' % (k, rendering.beststr(' ⇨', '->'),
v.strip()))


class Exit(command.Command):
Expand Down
13 changes: 0 additions & 13 deletions shellish/command/supplement.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,19 +106,6 @@ def _format_text(self, text):
def _get_help_string(self, action):
""" Adopted from ArgumentDefaultsHelpFormatter. """
raise NotImplementedError('')
help = action.help
prefix = ''
if getattr(action, 'env', None):
prefix = '(<cyan>%s</cyan>) ' % action.env
if '%(default)' not in help and \
action.default not in (argparse.SUPPRESS, None):
if action.option_strings and action.nargs != 0:
if isinstance(action.default, io.IOBase):
default = action.default.name
else:
default = action.default
prefix = '[<b>%s</b>] %s ' % (default, prefix)
return prefix, help

def _format_usage(self, *args, **kwargs):
usage = super()._format_usage(*args, **kwargs)
Expand Down
5 changes: 0 additions & 5 deletions shellish/session.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ def __init__(self, root_command, name=None):
self.root_command = root_command
self.name = name or root_command.name
self.config = self.load_config()
if 'alias' in self.config:
self.aliases = self.config['alias']
else:
self.aliases = {}
self.add_events(['precmd', 'postcmd'])
raw_prompt = self.config['ui']['prompt_format']
self.prompt_format = ast.literal_eval("'%s '" % raw_prompt)
Expand Down Expand Up @@ -220,7 +216,6 @@ def complete_wrap(self, func, *args, **kwargs):

def complete_names(self, text, line, begin, end):
choices = set(self.root_command.subcommands)
choices |= set(self.aliases)
return [x for x in choices if x.startswith(line)]

@contextlib.contextmanager
Expand Down

0 comments on commit bf34216

Please sign in to comment.