2020-12-06 23:03:17 +00:00
|
|
|
import argparse
|
|
|
|
import sys
|
|
|
|
import winshell
|
|
|
|
|
2021-02-26 03:42:35 +00:00
|
|
|
from voussoirkit import betterhelp
|
2020-12-06 23:03:17 +00:00
|
|
|
from voussoirkit import pathclass
|
2021-02-26 03:42:35 +00:00
|
|
|
from voussoirkit import subproctools
|
2020-12-06 23:03:17 +00:00
|
|
|
|
|
|
|
def shortcut(lnk_name, target, start_in=None, icon=None):
|
|
|
|
lnk = pathclass.Path(lnk_name)
|
|
|
|
if lnk.extension != 'lnk':
|
|
|
|
lnk = lnk.add_extension('lnk')
|
|
|
|
|
|
|
|
lnk.assert_not_exists()
|
|
|
|
|
2021-02-26 03:42:35 +00:00
|
|
|
(target, args) = (target[0], target[1:])
|
2020-12-06 23:03:17 +00:00
|
|
|
target = pathclass.Path(target)
|
|
|
|
target.assert_exists()
|
|
|
|
|
|
|
|
if start_in is not None:
|
|
|
|
start_in = pathclass.Path(start_in)
|
|
|
|
start_in.assert_is_directory()
|
|
|
|
|
|
|
|
if icon is not None:
|
|
|
|
icon = pathclass.Path(icon)
|
|
|
|
icon.assert_is_file()
|
|
|
|
|
2020-12-07 08:29:44 +00:00
|
|
|
shortcut = winshell.Shortcut(lnk.absolute_path)
|
|
|
|
shortcut.path = target.absolute_path
|
2021-02-26 03:42:35 +00:00
|
|
|
|
|
|
|
if args:
|
|
|
|
shortcut.arguments = subproctools.format_command(args)
|
2020-12-06 23:03:17 +00:00
|
|
|
if start_in is not None:
|
2020-12-07 08:29:44 +00:00
|
|
|
shortcut.working_directory = start_in.absolute_path
|
2020-12-06 23:03:17 +00:00
|
|
|
if icon is not None:
|
2020-12-07 08:29:44 +00:00
|
|
|
shortcut.icon_location = (icon.absolute_path, 0)
|
2020-12-06 23:03:17 +00:00
|
|
|
|
2020-12-07 08:29:44 +00:00
|
|
|
shortcut.write()
|
|
|
|
return lnk
|
2020-12-06 23:03:17 +00:00
|
|
|
|
2021-02-26 03:42:35 +00:00
|
|
|
DOCSTRING = '''
|
|
|
|
shortcut
|
|
|
|
========
|
|
|
|
|
|
|
|
> shortcut lnk_path target <flags>
|
|
|
|
|
|
|
|
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.
|
|
|
|
'''
|
|
|
|
|
2020-12-06 23:03:17 +00:00
|
|
|
def shortcut_argparse(args):
|
2020-12-07 08:29:44 +00:00
|
|
|
try:
|
|
|
|
lnk = shortcut(
|
|
|
|
lnk_name=args.lnk_name,
|
|
|
|
target=args.target,
|
|
|
|
start_in=args.start_in,
|
|
|
|
icon=args.icon,
|
|
|
|
)
|
|
|
|
print(lnk.absolute_path)
|
|
|
|
return 0
|
|
|
|
except pathclass.Exists:
|
|
|
|
print(f'{args.lnk_name} already exists.')
|
|
|
|
return 1
|
2020-12-06 23:03:17 +00:00
|
|
|
|
|
|
|
def main(argv):
|
|
|
|
parser = argparse.ArgumentParser(description=__doc__)
|
|
|
|
|
|
|
|
parser.add_argument('lnk_name')
|
2021-02-26 03:42:35 +00:00
|
|
|
parser.add_argument('target', nargs='+')
|
2021-02-21 05:01:55 +00:00
|
|
|
parser.add_argument('--start_in', '--start-in', '--startin', default=None)
|
|
|
|
parser.add_argument('--icon', default=None)
|
2020-12-06 23:03:17 +00:00
|
|
|
parser.set_defaults(func=shortcut_argparse)
|
|
|
|
|
2021-02-26 03:42:35 +00:00
|
|
|
return betterhelp.single_main(argv, parser, DOCSTRING)
|
2020-12-06 23:03:17 +00:00
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
raise SystemExit(main(sys.argv[1:]))
|