From af9d797b0ecb8efbdba16eb869f754075e4f4ac6 Mon Sep 17 00:00:00 2001 From: Ethan Dalool Date: Thu, 25 Feb 2021 19:42:35 -0800 Subject: [PATCH] Let shortcut.py take more target args, use betterhelp. --- shortcut.py | 34 +++++++++++++++++++++++++++++++--- 1 file changed, 31 insertions(+), 3 deletions(-) diff --git a/shortcut.py b/shortcut.py index 7e3e214..7ea26e9 100644 --- a/shortcut.py +++ b/shortcut.py @@ -2,7 +2,9 @@ import argparse import sys import winshell +from voussoirkit import betterhelp from voussoirkit import pathclass +from voussoirkit import subproctools def shortcut(lnk_name, target, start_in=None, icon=None): lnk = pathclass.Path(lnk_name) @@ -11,6 +13,7 @@ def shortcut(lnk_name, target, start_in=None, icon=None): lnk.assert_not_exists() + (target, args) = (target[0], target[1:]) target = pathclass.Path(target) target.assert_exists() @@ -24,6 +27,9 @@ def shortcut(lnk_name, target, start_in=None, icon=None): shortcut = winshell.Shortcut(lnk.absolute_path) shortcut.path = target.absolute_path + + if args: + shortcut.arguments = subproctools.format_command(args) if start_in is not None: shortcut.working_directory = start_in.absolute_path if icon is not None: @@ -32,6 +38,29 @@ def shortcut(lnk_name, target, start_in=None, icon=None): shortcut.write() return lnk +DOCSTRING = ''' +shortcut +======== + +> shortcut lnk_path target + +lnk_path: + The filepath of the lnk file you want to create. + +target: + The filepath of the target file and any additional arguments separated + by spaces. If you want to include an argument that starts with hyphens, + consider putting this last and use `--` to indicate the end of named + arguments. For example: + > shortcut game.lnk --icon game.ico -- javaw.exe -jar game.jar + +--start-in: + Directory to use as CWD for the program. + +--icon: + Path to an .ico file. +''' + def shortcut_argparse(args): try: lnk = shortcut( @@ -50,13 +79,12 @@ def main(argv): parser = argparse.ArgumentParser(description=__doc__) parser.add_argument('lnk_name') - parser.add_argument('target') + parser.add_argument('target', nargs='+') parser.add_argument('--start_in', '--start-in', '--startin', default=None) parser.add_argument('--icon', default=None) parser.set_defaults(func=shortcut_argparse) - args = parser.parse_args(argv) - return args.func(args) + return betterhelp.single_main(argv, parser, DOCSTRING) if __name__ == '__main__': raise SystemExit(main(sys.argv[1:]))