|
1 """EditorConfig command line interface |
|
2 |
|
3 Licensed under PSF License (see LICENSE.txt file). |
|
4 |
|
5 """ |
|
6 |
|
7 import getopt |
|
8 import sys |
|
9 |
|
10 from editorconfig import __version__, VERSION |
|
11 from editorconfig.compat import force_unicode |
|
12 from editorconfig.versiontools import split_version |
|
13 from editorconfig.handler import EditorConfigHandler |
|
14 from editorconfig.exceptions import ParsingError, PathError, VersionError |
|
15 |
|
16 |
|
17 def version(): |
|
18 print("EditorConfig Python Core Version %s" % __version__) |
|
19 |
|
20 |
|
21 def usage(command, error=False): |
|
22 if error: |
|
23 out = sys.stderr |
|
24 else: |
|
25 out = sys.stdout |
|
26 out.write("%s [OPTIONS] FILENAME\n" % command) |
|
27 out.write('-f ' |
|
28 'Specify conf filename other than ".editorconfig".\n') |
|
29 out.write("-b " |
|
30 "Specify version (used by devs to test compatibility).\n") |
|
31 out.write("-h OR --help Print this help message.\n") |
|
32 out.write("-v OR --version Display version information.\n") |
|
33 |
|
34 |
|
35 def main(): |
|
36 command_name = sys.argv[0] |
|
37 try: |
|
38 opts, args = getopt.getopt(list(map(force_unicode, sys.argv[1:])), |
|
39 "vhb:f:", ["version", "help"]) |
|
40 except getopt.GetoptError as e: |
|
41 print(str(e)) |
|
42 usage(command_name, error=True) |
|
43 sys.exit(2) |
|
44 |
|
45 version_tuple = VERSION |
|
46 conf_filename = '.editorconfig' |
|
47 |
|
48 for option, arg in opts: |
|
49 if option in ('-h', '--help'): |
|
50 usage(command_name) |
|
51 sys.exit() |
|
52 if option in ('-v', '--version'): |
|
53 version() |
|
54 sys.exit() |
|
55 if option == '-f': |
|
56 conf_filename = arg |
|
57 if option == '-b': |
|
58 version_tuple = split_version(arg) |
|
59 if version_tuple is None: |
|
60 sys.exit("Invalid version number: %s" % arg) |
|
61 |
|
62 if len(args) < 1: |
|
63 usage(command_name, error=True) |
|
64 sys.exit(2) |
|
65 filenames = args |
|
66 multiple_files = len(args) > 1 |
|
67 |
|
68 for filename in filenames: |
|
69 handler = EditorConfigHandler(filename, conf_filename, version_tuple) |
|
70 try: |
|
71 options = handler.get_configurations() |
|
72 except (ParsingError, PathError, VersionError) as e: |
|
73 print(str(e)) |
|
74 sys.exit(2) |
|
75 if multiple_files: |
|
76 print("[%s]" % filename) |
|
77 for key, value in options.items(): |
|
78 print("%s=%s" % (key, value)) |