1 from distutils
.errors
import DistutilsOptionError
3 from setuptools
.command
.setopt
import edit_config
, option_base
, config_file
7 """Quote an argument for later parsing by shlex.split()"""
8 for c
in '"', "'", "\\", "#":
11 if arg
.split() != [arg
]:
16 class alias(option_base
):
17 """Define a shortcut that invokes one or more commands"""
19 description
= "define a shortcut to invoke one or more commands"
20 command_consumes_arguments
= True
23 ('remove', 'r', 'remove (unset) the alias'),
24 ] + option_base
.user_options
26 boolean_options
= option_base
.boolean_options
+ ['remove']
28 def initialize_options(self
):
29 option_base
.initialize_options(self
)
33 def finalize_options(self
):
34 option_base
.finalize_options(self
)
35 if self
.remove
and len(self
.args
) != 1:
36 raise DistutilsOptionError(
37 "Must specify exactly one argument (the alias name) when "
42 aliases
= self
.distribution
.get_option_dict('aliases')
45 print("Command Aliases")
46 print("---------------")
48 print("setup.py alias", format_alias(alias
, aliases
))
51 elif len(self
.args
) == 1:
55 elif alias
in aliases
:
56 print("setup.py alias", format_alias(alias
, aliases
))
59 print("No alias definition found for %r" % alias
)
63 command
= ' '.join(map(shquote
, self
.args
[1:]))
65 edit_config(self
.filename
, {'aliases': {alias
: command
}}, self
.dry_run
)
68 def format_alias(name
, aliases
):
69 source
, command
= aliases
[name
]
70 if source
== config_file('global'):
71 source
= '--global-config '
72 elif source
== config_file('user'):
73 source
= '--user-config '
74 elif source
== config_file('local'):
77 source
= '--filename=%r' % source
78 return source
+ name
+ ' ' + command