|
7 | 7 | .. sourcecode:: console |
8 | 8 |
|
9 | 9 | $ sassc |
10 | | - Usage: sassc [options] SCSS_FILE... |
| 10 | + Usage: sassc [options] SCSS_FILE [CSS_FILE] |
11 | 11 |
|
12 | 12 | There are options as well: |
13 | 13 |
|
|
25 | 25 |
|
26 | 26 | Path to find images. Default is the current directory (:file:`./`). |
27 | 27 |
|
| 28 | +.. option:: -m, -g, --sourcemap |
| 29 | +
|
| 30 | + Emit source map. Requires the second argument (output CSS filename). |
| 31 | + The filename of source map will be the output CSS filename followed by |
| 32 | + :file:`.map`. |
| 33 | +
|
| 34 | + .. versionadded:: 0.4.0 |
| 35 | +
|
28 | 36 | .. option:: -v, --version |
29 | 37 |
|
30 | 38 | Prints the program version. |
|
38 | 46 | """ |
39 | 47 | from __future__ import print_function |
40 | 48 |
|
| 49 | +import functools |
41 | 50 | import optparse |
42 | 51 | import sys |
43 | 52 |
|
44 | 53 | from sass import __version__ as VERSION, OUTPUT_STYLES, CompileError, compile |
45 | 54 |
|
46 | 55 |
|
47 | 56 | def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr): |
48 | | - parser = optparse.OptionParser(usage='%prog [options] SCSS_FILE...', |
49 | | - version='%prog ' + VERSION) |
| 57 | + parser = optparse.OptionParser( |
| 58 | + usage='%prog [options] SCSS_FILE [OUT_CSS_FILE]', |
| 59 | + version='%prog ' + VERSION |
| 60 | + ) |
50 | 61 | output_styles = list(OUTPUT_STYLES) |
51 | 62 | output_styles = ', '.join(output_styles[:-1]) + ', or ' + output_styles[-1] |
52 | 63 | parser.add_option('-s', '--output-style', metavar='STYLE', type='choice', |
53 | 64 | choices=list(OUTPUT_STYLES), default='nested', |
54 | 65 | help='Coding style of the compiled result. Choose one ' |
55 | 66 | 'of ' + output_styles + '. [default: %default]') |
| 67 | + parser.add_option('-m', '-g', '--sourcemap', dest='source_map', |
| 68 | + action='store_true', default=False, |
| 69 | + help='Emit source map. Requires the second argument ' |
| 70 | + '(output css filename).') |
56 | 71 | parser.add_option('-I', '--include-path', metavar='DIR', |
57 | 72 | dest='include_paths', action='append', |
58 | 73 | help='Path to find "@import"ed (S)CSS source files. ' |
59 | 74 | 'Can be multiply used.') |
60 | 75 | parser.add_option('-i', '--image-path', metavar='DIR', default='./', |
61 | 76 | help='Path to find images. [default: %default]') |
62 | 77 | options, args = parser.parse_args(argv[1:]) |
| 78 | + error = functools.partial(print, |
| 79 | + parser.get_prog_name() + ': error:', |
| 80 | + file=stderr) |
63 | 81 | if not args: |
64 | 82 | parser.print_usage(stderr) |
65 | | - print(parser.get_prog_name() + ': error:', 'too few arguments', |
66 | | - file=stderr) |
| 83 | + error('too few arguments') |
67 | 84 | return 2 |
68 | | - elif len(args) > 1: |
| 85 | + elif len(args) > 2: |
69 | 86 | parser.print_usage(stderr) |
70 | | - print(parser.get_prog_name() + ': error:', 'too many arguments', |
71 | | - file=stderr) |
| 87 | + error('too many arguments') |
72 | 88 | return 2 |
73 | | - for filename in args: |
74 | | - try: |
75 | | - css = compile( |
| 89 | + filename = args[0] |
| 90 | + if options.source_map and len(args) < 2: |
| 91 | + parser.print_usage(stderr) |
| 92 | + error('-m/-g/--sourcemap requires the second argument, the output ' |
| 93 | + 'css filename.') |
| 94 | + return 2 |
| 95 | + try: |
| 96 | + if options.source_map: |
| 97 | + source_map_filename = args[1] + '.map' # FIXME |
| 98 | + css, source_map = compile( |
76 | 99 | filename=filename, |
77 | 100 | output_style=options.output_style, |
| 101 | + source_comments='map', |
| 102 | + source_map_filename=source_map_filename, |
78 | 103 | include_paths=options.include_paths, |
79 | 104 | image_path=options.image_path |
80 | 105 | ) |
81 | | - except CompileError as e: |
82 | | - print(parser.get_prog_name() + ': error:', e, |
83 | | - file=stderr) |
84 | | - return 1 |
85 | 106 | else: |
| 107 | + source_map_filename = None |
| 108 | + source_map = None |
| 109 | + css = compile( |
| 110 | + filename=filename, |
| 111 | + output_style=options.output_style, |
| 112 | + include_paths=options.include_paths, |
| 113 | + image_path=options.image_path |
| 114 | + ) |
| 115 | + except CompileError as e: |
| 116 | + print(parser.get_prog_name() + ': error:', e, |
| 117 | + file=stderr) |
| 118 | + return 1 |
| 119 | + else: |
| 120 | + if len(args) < 2: |
86 | 121 | print(css, file=stdout) |
| 122 | + else: |
| 123 | + with open(args[1], 'w') as f: |
| 124 | + print(css, file=f) |
| 125 | + if source_map_filename: |
| 126 | + with open(source_map_filename, 'w') as f: |
| 127 | + f.write(source_map) |
87 | 128 | return 0 |
88 | 129 |
|
89 | 130 |
|
|
0 commit comments