Skip to content
Snippets Groups Projects
Commit 1ef15d25 authored by Ramon Buldó's avatar Ramon Buldó
Browse files

Improve --help output

parent ea216793
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# This file is part of pacman-mirrors.
#
# pacman-mirrors is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# pacman-mirrors is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with pacman-mirrors. If not, see <http://www.gnu.org/licenses/>.
import argparse
class CustomHelpFormatter(argparse.HelpFormatter):
"""Provide a customized format for help output.
http://stackoverflow.com/questions/9642692/argparse-help-without-duplicate-allcaps
https://github.com/Ecogenomics/mingle/blob/master/mingle/custom_help_formatter.py
"""
def _split_lines(self, text, width):
return text.splitlines()
def _get_help_string(self, action):
"""Place default value in help string."""
h = action.help
if '%(default)' not in action.help:
if action.default != '' and action.default != [] and action.default != None and action.default != False:
if action.default is not argparse.SUPPRESS:
defaulting_nargs = [argparse.OPTIONAL, argparse.ZERO_OR_MORE]
if action.option_strings or action.nargs in defaulting_nargs:
if '\n' in h:
lines = h.splitlines()
lines[0] += ' (default: %(default)s)'
h = '\n'.join(lines)
else:
h += ' (default: %(default)s)'
return h
def _fill_text(self, text, width, indent):
"""Permit multiple line descriptions."""
return ''.join([indent + line for line in text.splitlines(True)])
def _format_action_invocation(self, action):
"""Removes duplicate ALLCAPS with positional arguments."""
if not action.option_strings:
default = self._get_default_metavar_for_positional(action)
metavar, = self._metavar_formatter(action, default)(1)
return metavar
else:
parts = []
# if the Optional doesn't take a value, format is:
# -s, --long
if action.nargs == 0:
parts.extend(action.option_strings)
# if the Optional takes a value, format is:
# -s ARGS, --long ARGS
else:
default = self._get_default_metavar_for_optional(action)
args_string = self._format_args(action, default)
for option_string in action.option_strings:
parts.append(option_string)
return '%s %s' % (', '.join(parts), args_string)
return ', '.join(parts)
def _get_default_metavar_for_optional(self, action):
return action.dest.upper()
def _get_default_metavar_for_positional(self, action):
return action.dest
......@@ -34,6 +34,7 @@ import os
import importlib.util
from pacman_mirrors_gui import chooseMirrors
from custom_help_formatter import CustomHelpFormatter
def print_write_error(e):
......@@ -117,35 +118,42 @@ class PacmanMirrors:
else:
gtk_available = True
parser = argparse.ArgumentParser()
parser = argparse.ArgumentParser(formatter_class=CustomHelpFormatter)
parser.add_argument("-g", "--generate",
help="generate new mirrorlist",
action="store_true")
action="store_true",
help="generate new mirrorlist")
parser.add_argument("-m", "--method",
help="use generation method",
type=str, choices=["rank", "random"])
type=str,
choices=["rank", "random"],
help="generation method")
parser.add_argument("-b", "--branch",
help="use branch name",
type=str, choices=["stable", "testing", "unstable"])
type=str,
choices=["stable", "testing", "unstable"],
help="branch name")
parser.add_argument("-c", "--country",
help="use only mirrors from country[,country,...]",
type=str)
type=str,
help="comma separated list of countries "
"where mirrors will be used")
parser.add_argument("-d", "--mirror_dir",
help="use path as mirrorlist directory",
type=str)
type=str,
metavar="PATH",
help="mirrors list path")
parser.add_argument("-o", "--output",
help="specify output file",
type=str)
type=str,
metavar='FILE',
help="output file")
parser.add_argument("-t", "--timeout",
help="server maximum waiting time (seconds)",
type=int)
type=int,
metavar="SECONDS",
help="server maximum waiting time")
if gtk_available:
parser.add_argument("-i", "--interactive",
help="interactively generate a custom mirrorlist",
action="store_true")
action="store_true",
help="interactively generate a custom "
"mirrorlist")
parser.add_argument("-v", "--version",
help="print the pacman-mirrors version",
action="store_true")
action="store_true",
help="print the pacman-mirrors version")
args = parser.parse_args()
if len(sys.argv) == 1:
......@@ -153,7 +161,7 @@ class PacmanMirrors:
exit(0)
if args.version:
print("pacman-mirrors 1.5")
print("pacman-mirrors 20150808")
exit(0)
if args.generate:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment