mirror of
https://github.com/fish-shell/fish-shell
synced 2024-12-29 06:13:20 +00:00
create_manpage_completions: Switch to argparse
This is a lot cleaner and more easily extendable.
This commit is contained in:
parent
6e95e1d79d
commit
af03f2ce6d
1 changed files with 79 additions and 79 deletions
|
@ -16,7 +16,8 @@ Redistributions in binary form must reproduce the above copyright notice, this l
|
||||||
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import string, sys, re, os.path, bz2, gzip, traceback, getopt, errno, codecs
|
import string, sys, re, os.path, bz2, gzip, traceback, errno, codecs
|
||||||
|
import argparse
|
||||||
from deroff import Deroffer
|
from deroff import Deroffer
|
||||||
|
|
||||||
lzma_available = True
|
lzma_available = True
|
||||||
|
@ -994,104 +995,103 @@ def get_paths_from_man_locations():
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
def usage(script_name):
|
|
||||||
print(
|
|
||||||
"Usage: {0} [-v, --verbose] [-s, --stdout] [-d, --directory] [-p, --progress] [-k, --keep] files...".format(
|
|
||||||
script_name
|
|
||||||
)
|
|
||||||
)
|
|
||||||
print(
|
|
||||||
"""Command options are:
|
|
||||||
-h, --help\t\tShow this help message
|
|
||||||
-v, --verbose [0, 1, 2]\tShow debugging output to stderr. Larger is more verbose.
|
|
||||||
-s, --stdout\tWrite all completions to stdout (trumps the --directory option)
|
|
||||||
-d, --directory [dir]\tWrite all completions to the given directory, instead of to ~/.local/share/fish/generated_completions
|
|
||||||
-m, --manpath\tProcess all man1 and man8 files available in the manpath (as determined by manpath)
|
|
||||||
-p, --progress\tShow progress
|
|
||||||
-k, --keep\tDo not remove files from destination directory.
|
|
||||||
"""
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
script_name = sys.argv[0]
|
script_name = sys.argv[0]
|
||||||
try:
|
parser = argparse.ArgumentParser(
|
||||||
opts, file_paths = getopt.gnu_getopt(
|
description="create_manpage_completions: Generate fish-shell completions from manpages"
|
||||||
sys.argv[1:],
|
|
||||||
"v:kd:hmpc:z",
|
|
||||||
[
|
|
||||||
"verbose=",
|
|
||||||
"stdout",
|
|
||||||
"directory=",
|
|
||||||
"cleanup-in=",
|
|
||||||
"help",
|
|
||||||
"manpath",
|
|
||||||
"progress",
|
|
||||||
"keep",
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
except getopt.GetoptError as err:
|
parser.add_argument(
|
||||||
print(err.msg) # will print something like "option -a not recognized"
|
"-c",
|
||||||
usage(script_name)
|
"--cleanup-in",
|
||||||
sys.exit(2)
|
type=str,
|
||||||
|
help="Directories to clean up in",
|
||||||
|
action="append",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-d",
|
||||||
|
"--directory",
|
||||||
|
type=str,
|
||||||
|
help="The directory to save the completions in",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-k",
|
||||||
|
"--keep",
|
||||||
|
help="Whether to keep files in the target directory",
|
||||||
|
action="store_true",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-m",
|
||||||
|
"--manpath",
|
||||||
|
help="Whether to use manpath",
|
||||||
|
action="store_true",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-p",
|
||||||
|
"--progress",
|
||||||
|
help="Whether to show progress",
|
||||||
|
action="store_true",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-s",
|
||||||
|
"--stdout",
|
||||||
|
help="Write the completions to stdout",
|
||||||
|
action="store_true",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-v",
|
||||||
|
"--verbose",
|
||||||
|
type=int,
|
||||||
|
choices=[0, 1, 2],
|
||||||
|
help="The level of debug output to show",
|
||||||
|
)
|
||||||
|
parser.add_argument(
|
||||||
|
"-z",
|
||||||
|
"--deroff-only",
|
||||||
|
help="Whether to just deroff",
|
||||||
|
action="store_true",
|
||||||
|
)
|
||||||
|
parser.add_argument('file_paths', type=str, nargs='*')
|
||||||
|
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
if args.verbose:
|
||||||
|
VERBOSITY = args.verbose
|
||||||
|
if args.stdout:
|
||||||
|
WRITE_TO_STDOUT = True
|
||||||
|
if args.deroff_only:
|
||||||
|
DEROFF_ONLY = True
|
||||||
|
if args.keep:
|
||||||
|
KEEP_FILES = True
|
||||||
|
if args.manpath:
|
||||||
|
# Fetch all man1 and man8 files from the manpath or man.conf
|
||||||
|
args.file_paths.extend(get_paths_from_man_locations())
|
||||||
|
|
||||||
# Directories within which we will clean up autogenerated completions
|
# Directories within which we will clean up autogenerated completions
|
||||||
# This script originally wrote completions into ~/.config/fish/completions
|
# This script originally wrote completions into ~/.config/fish/completions
|
||||||
# Now it writes them into a separate directory
|
# Now it writes them into a separate directory
|
||||||
cleanup_directories = []
|
if args.cleanup_in:
|
||||||
|
for cleanup_dir in args.cleanup_in:
|
||||||
use_manpath, show_progress, custom_dir = False, False, False
|
|
||||||
output_directory = ""
|
|
||||||
for opt, value in opts:
|
|
||||||
if opt in ("-v", "--verbose"):
|
|
||||||
VERBOSITY = int(value)
|
|
||||||
elif opt in ("-s", "--stdout"):
|
|
||||||
WRITE_TO_STDOUT = True
|
|
||||||
elif opt in ("-d", "--directory"):
|
|
||||||
output_directory = value
|
|
||||||
elif opt in ("-h", "--help"):
|
|
||||||
usage(script_name)
|
|
||||||
sys.exit(0)
|
|
||||||
elif opt in ("-m", "--manpath"):
|
|
||||||
use_manpath = True
|
|
||||||
elif opt in ("-p", "--progress"):
|
|
||||||
show_progress = True
|
|
||||||
elif opt in ("-c", "--cleanup-in"):
|
|
||||||
cleanup_directories.append(value)
|
|
||||||
elif opt in ("-z",):
|
|
||||||
DEROFF_ONLY = True
|
|
||||||
elif opt in ("-k", "--keep"):
|
|
||||||
KEEP_FILES = True
|
|
||||||
else:
|
|
||||||
assert False, "unhandled option"
|
|
||||||
|
|
||||||
if use_manpath:
|
|
||||||
# Fetch all man1 and man8 files from the manpath or man.conf
|
|
||||||
file_paths.extend(get_paths_from_man_locations())
|
|
||||||
|
|
||||||
if cleanup_directories:
|
|
||||||
for cleanup_dir in cleanup_directories:
|
|
||||||
cleanup_autogenerated_completions_in_directory(cleanup_dir)
|
cleanup_autogenerated_completions_in_directory(cleanup_dir)
|
||||||
|
|
||||||
if not file_paths:
|
if not args.file_paths:
|
||||||
print("No paths specified")
|
print("No paths specified")
|
||||||
sys.exit(0)
|
sys.exit(0)
|
||||||
|
|
||||||
if not WRITE_TO_STDOUT and not output_directory:
|
if not args.stdout and not args.directory:
|
||||||
# Default to ~/.local/share/fish/generated_completions/
|
# Default to ~/.local/share/fish/generated_completions/
|
||||||
# Create it if it doesn't exist
|
# Create it if it doesn't exist
|
||||||
xdg_data_home = os.getenv("XDG_DATA_HOME", "~/.local/share")
|
xdg_data_home = os.getenv("XDG_DATA_HOME", "~/.local/share")
|
||||||
output_directory = os.path.expanduser(
|
args.directory = os.path.expanduser(
|
||||||
xdg_data_home + "/fish/generated_completions/"
|
xdg_data_home + "/fish/generated_completions/"
|
||||||
)
|
)
|
||||||
try:
|
try:
|
||||||
os.makedirs(output_directory)
|
os.makedirs(args.directory)
|
||||||
except OSError as e:
|
except OSError as e:
|
||||||
if e.errno != errno.EEXIST:
|
if e.errno != errno.EEXIST:
|
||||||
raise
|
raise
|
||||||
|
|
||||||
if not WRITE_TO_STDOUT and not KEEP_FILES:
|
if not args.stdout and not args.keep:
|
||||||
# Remove old generated files
|
# Remove old generated files
|
||||||
cleanup_autogenerated_completions_in_directory(output_directory)
|
cleanup_autogenerated_completions_in_directory(args.directory)
|
||||||
|
|
||||||
parse_and_output_man_pages(file_paths, output_directory, show_progress)
|
parse_and_output_man_pages(args.file_paths, args.directory, args.progress)
|
||||||
|
|
Loading…
Reference in a new issue