|
| 1 | +""":mod:`sassc` --- SassC compliant command line interface |
| 2 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 3 | +
|
| 4 | +This provides SassC_ compliant CLI executable named :program:`sassc`: |
| 5 | +
|
| 6 | +.. sourcecode:: console |
| 7 | +
|
| 8 | + $ sassc |
| 9 | + Usage: sassc [options] SCSS_FILE... |
| 10 | +
|
| 11 | +There are options as well: |
| 12 | +
|
| 13 | +.. option:: -s <style>, --output-style <style> |
| 14 | +
|
| 15 | + Coding style of the compiled result. The same as :func:`sass.compile()` |
| 16 | + function's ``output_style`` keyword argument. Default is ``nested``. |
| 17 | +
|
| 18 | +.. option:: -I <dir>, --include-path <dir> |
| 19 | +
|
| 20 | + Optional directory path to find ``@import``\ ed (S)CSS files. |
| 21 | + Can be multiply used. |
| 22 | +
|
| 23 | +.. option:: -i <dir>, --image-path <dir> |
| 24 | +
|
| 25 | + Path to find images. Default is the current directory (:file:`./`). |
| 26 | +
|
| 27 | +.. option:: -v, --version |
| 28 | +
|
| 29 | + Prints the program version. |
| 30 | +
|
| 31 | +.. option:: -h, --help |
| 32 | +
|
| 33 | + Prints the help message. |
| 34 | +
|
| 35 | +.. _SassC: https://github.com/hcatlin/sassc |
| 36 | +
|
| 37 | +""" |
| 38 | +import optparse |
| 39 | +import sys |
| 40 | + |
| 41 | +from sass import __version__ as VERSION, OUTPUT_STYLES, CompileError, compile |
| 42 | + |
| 43 | + |
| 44 | +def main(argv=sys.argv, stdout=sys.stdout, stderr=sys.stderr): |
| 45 | + parser = optparse.OptionParser(usage='%prog [options] SCSS_FILE...', |
| 46 | + version='%prog ' + VERSION) |
| 47 | + output_styles = list(OUTPUT_STYLES) |
| 48 | + output_styles = ', '.join(output_styles[:-1]) + ', or ' + output_styles[-1] |
| 49 | + parser.add_option('-s', '--output-style', metavar='STYLE', type='choice', |
| 50 | + choices=list(OUTPUT_STYLES), default='nested', |
| 51 | + help='Coding style of the compiled result. Choose one ' |
| 52 | + 'of ' + output_styles + '. [default: %default]') |
| 53 | + parser.add_option('-I', '--include-path', metavar='DIR', |
| 54 | + dest='include_paths', action='append', |
| 55 | + help='Path to find "@import"ed (S)CSS source files. ' |
| 56 | + 'Can be multiply used.') |
| 57 | + parser.add_option('-i', '--image-path', metavar='DIR', default='./', |
| 58 | + help='Path to find images. [default: %default]') |
| 59 | + options, args = parser.parse_args(argv[1:]) |
| 60 | + if not args: |
| 61 | + parser.print_usage(stderr) |
| 62 | + print >> stderr, parser.get_prog_name() + ': error:', \ |
| 63 | + 'too few arguments' |
| 64 | + return 2 |
| 65 | + elif len(args) > 1: |
| 66 | + parser.print_usage(stderr) |
| 67 | + print >> stderr, parser.get_prog_name() + ': error:', \ |
| 68 | + 'too many arguments' |
| 69 | + return 2 |
| 70 | + for filename in args: |
| 71 | + try: |
| 72 | + css = compile( |
| 73 | + filename=filename, |
| 74 | + output_style=options.output_style, |
| 75 | + include_paths=options.include_paths, |
| 76 | + image_path=options.image_path |
| 77 | + ) |
| 78 | + except CompileError as e: |
| 79 | + print >> stderr, parser.get_prog_name() + ': error:', e |
| 80 | + return 1 |
| 81 | + else: |
| 82 | + print css |
| 83 | + return 0 |
| 84 | + |
| 85 | + |
| 86 | +if __name__ == '__main__': |
| 87 | + sys.exit(main()) |
0 commit comments