Commit 3253f145 authored by Brett Smith's avatar Brett Smith Committed by Mattia Rizzolo

diffoscope: Specify choices for --list-tools switch.

This gives the user more information about what they can specify as an
argument, and generates a nicer error message if they give an unrecognized
argument.

With that done, we can simplify the implementation of ListToolsAction,
because argparse has already validated the argument.
Signed-off-by: Mattia Rizzolo's avatarMattia Rizzolo <mattia@debian.org>
parent ad9182e7
......@@ -62,7 +62,11 @@ def create_parser():
parser.add_argument('--version', action='version',
version='diffoscope %s' % VERSION)
parser.add_argument('--list-tools', nargs='?', type=str, action=ListToolsAction,
help='Show external tools required and exit')
metavar='DISTRO', choices=sorted(OS_NAMES),
help='Show external tools required and exit. '
'DISTRO can be one of {%(choices)s}. '
'If specified, the output will list packages in that '
'distribution that satisfy these dependencies.')
parser.add_argument('--debug', dest='debug', action='store_true',
default=False, help='Display debug messages')
parser.add_argument('--debugger', action='store_true',
......@@ -184,20 +188,12 @@ class ListToolsAction(argparse.Action):
print("External-Tools-Required: ", end='')
print(', '.join(sorted(tool_required.all)))
if os_override:
if os_override in OS_NAMES.keys():
os_list = [os_override]
else:
print()
print("No package mapping found for: {} (possible values: {})".format(os_override, ", ".join(sorted(OS_NAMES.keys()))), file=sys.stderr)
sys.exit(1)
os_list = [os_override]
else:
current_os = get_current_os()
if current_os in OS_NAMES.keys():
os_list = [current_os]
else:
os_list = OS_NAMES.keys()
os_list = [current_os] if (current_os in OS_NAMES) else iter(OS_NAMES)
for os in os_list:
print("Available-in-{}-packages: ".format(OS_NAMES.get(os, os)), end='')
print("Available-in-{}-packages: ".format(OS_NAMES[os]), end='')
print(', '.join(sorted(filter(None, {
EXTERNAL_TOOLS.get(k, {}).get(os, None)
for k in tool_required.all
......
......@@ -17,6 +17,7 @@
# You should have received a copy of the GNU General Public License
# along with diffoscope. If not, see <https://www.gnu.org/licenses/>.
import collections
import platform
import functools
......@@ -28,11 +29,13 @@ from .profiling import profile
# calls
find_executable = functools.lru_cache()(find_executable)
OS_NAMES = {
'arch': 'Arch Linux',
'debian': 'Debian',
'FreeBSD': 'FreeBSD',
}
# The output of --help and --list-tools will use the order of this dict.
# Please keep it alphabetized.
OS_NAMES = collections.OrderedDict([
('arch', 'Arch Linux'),
('debian', 'Debian'),
('FreeBSD', 'FreeBSD'),
])
def tool_required(command):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment