Make python shell return error codes

Return result 1 for error cases
Output usage on stderr in error cases

Related to #209
This commit is contained in:
Liam Newman 2013-04-15 16:05:23 -07:00
parent bf09af14e6
commit a11b602f50
3 changed files with 27 additions and 23 deletions

View File

@ -2,6 +2,6 @@
#
# Stub script to run jsbeautifier
#
import sys
from jsbeautifier import main
main()
sys.exit(main())

View File

@ -1,3 +1,4 @@
from __future__ import print_function
import sys
import getopt
import re
@ -149,7 +150,7 @@ def beautify_file(file_name, opts = default_options() ):
return b.beautify(''.join(f.readlines()), opts)
def usage():
def usage(stream=sys.stdout):
print("""Javascript beautifier (http://jsbeautifier.org/)
@ -187,8 +188,11 @@ Rarely needed options:
-h, --help, --usage prints this help statement.
""")
""", file=stream)
if stream == sys.stderr:
return 1
else:
return 0
@ -1247,8 +1251,9 @@ def main():
'jslint-happy', 'brace-style=',
'keep-array-indentation', 'indent-level=', 'unescape-strings', 'help',
'usage', 'stdin', 'eval-code', 'indent-with-tabs', 'keep-function-indentation'])
except getopt.GetoptError:
return usage()
except getopt.GetoptError as ex:
print(ex.msg, file=sys.stderr)
return usage(sys.stderr)
js_options = default_options()
@ -1287,9 +1292,10 @@ def main():
elif opt in ('--help', '--usage', '-h'):
return usage()
if not file:
print("Must define at least one file.")
return usage()
print("Must define at least one file.", file=sys.stderr)
return usage(sys.stderr)
else:
if outfile == 'stdout':
print(beautify_file(file, js_options))

View File

@ -17,27 +17,25 @@ test_cli_common()
exit 1
}
# Not working yet for python
# $CLI_SCRIPT 2> /dev/null && {
# echo "[$CLI_SCRIPT_NAME (with no parameters)] Return code should be error."
# exit 1
# }
$CLI_SCRIPT 2> /dev/null && {
echo "[$CLI_SCRIPT_NAME (with no parameters)] Return code should be error."
exit 1
}
# Not working yet for python
# $CLI_SCRIPT -invalidParameter 2> /dev/null && {
# echo "[$CLI_SCRIPT_NAME -invalidParameter] Return code should be error."
# exit 1
# }
$CLI_SCRIPT -invalidParameter 2> /dev/null && {
echo "[$CLI_SCRIPT_NAME -invalidParameter] Return code should be error."
exit 1
}
$CLI_SCRIPT -h > /dev/null || {
echo "[$CLI_SCRIPT_NAME -h] Return code should be success."
exit 1
}
$CLI_SCRIPT -v > /dev/null || {
echo "[$CLI_SCRIPT_NAME -v] Return code should be success."
exit 1
}
# $CLI_SCRIPT -v > /dev/null || {
# echo "[$CLI_SCRIPT_NAME -v] Return code should be success."
# exit 1
# }
}