Created
August 25, 2014 20:15
-
-
Save EvergreenTheTree/028b979595e7b7998277 to your computer and use it in GitHub Desktop.
Just a quick command line number/string sorter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/python | |
from sys import argv | |
from sys import exit | |
help = '''Usage: sort [OPTION]... [ARGS]\n | |
[OPTIONS] | |
\'-a\' - Sort ARGS alphabetically. | |
\'-i\' - Sort ARGS numerically. | |
\'-b\' - Sort ARGS alphabetically | |
\'--help\' - Display this dialog.''' | |
error = '''Usage: sort [OPTION]... [ARGS] | |
Try 'sort --help' for more information.''' | |
error_modeint = 'ERROR: With option \'-i\' all arguments following must be ints.\n' | |
error_modestr = 'ERROR: With option \'-a\' all arguments following must be strs or chars.\n' | |
error_modeboth = 'ERROR: With option \'-b\' all arguments following must be ints, strs or chars.\n' | |
error_args = 'ERROR: %s is not a valid option.\n' | |
if len(argv) == 1: | |
print error | |
exit(1) | |
arglist = [] | |
args = argv[1].split('-') | |
if len(args) < 2: | |
args.insert(0, '') | |
elif len(args) > 2: | |
args.remove('') | |
mode = 'both' | |
if args[1] == 'a': | |
mode = 'str' | |
if len(argv) < 3: | |
print error_modestr | |
print error | |
exit(1) | |
elif args[1] == 'i': | |
mode = 'int' | |
if len(argv) < 3: | |
print error_modeint | |
print error | |
exit(1) | |
elif args[1] == 'help': | |
mode = 'help' | |
elif args[1] == 'b': | |
mode = 'both' | |
if len(argv) < 3: | |
print error_modeboth | |
print error | |
exit(1) | |
else: | |
print error_args % str(argv[1]) | |
print error | |
exit(1) | |
if mode == 'int': | |
for i in range(2, len(argv)): | |
try: | |
arglist.append(int(argv[i])) | |
except: | |
print error_modeint | |
print error | |
exit(1) | |
arglist.sort(key=int) | |
elif mode == 'str': | |
for i in range(2, len(argv)): | |
try: | |
arglist.append(str(argv[i])) | |
except: | |
print error_modestr | |
print error | |
exit(1) | |
arglist.sort() | |
elif mode == 'both': | |
for i in range(1, len(argv)): | |
try: | |
arglist.append(str(argv[i])) | |
except: | |
print error_modeboth | |
print error | |
exit(1) | |
arglist.sort() | |
if mode == 'help': | |
print help | |
else: | |
for i in arglist: | |
print i | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment