Created
January 26, 2015 16:03
-
-
Save razasyedh/14dbe9018b2c863e8763 to your computer and use it in GitHub Desktop.
A utility for converting strings to different cases.
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/env python | |
"""case.py | |
A utility for converting strings to different cases. | |
Usage info is autogenerated by the argparse module and can be accessed | |
by running the program with the '-h' option. | |
""" | |
import sys | |
import argparse | |
import string | |
def get_args(): | |
"""Obtain the arguments entered for our program on the command line.""" | |
# Define all of our arguments and their help messages | |
description = ("case - A utility for converting strings to different" | |
" cases.") | |
epilog = ("Note: Leading, trailing, and multiple whitespace is not" | |
" preserved for -C/-t.") | |
parser = argparse.ArgumentParser(add_help=False, description=description, | |
epilog=epilog) | |
# If we're running interactively, require a string | |
if sys.stdin.isatty(): | |
parser.add_argument("string", help="The string to convert.") | |
cases = parser.add_mutually_exclusive_group(required=True) | |
cases.add_argument("-c", "--capitalize", action="store_true", | |
help="Capitalize the first letter of the string.") | |
cases.add_argument("-C", "--capwords", action="store_true", | |
help="Capitalize every word.") | |
cases.add_argument("-t", "--titlecase", action="store_true", | |
help="Capitalize words according to titlecase" | |
" rules.") | |
cases.add_argument("-u", "--uppercase", action="store_true", | |
help="Make every letter uppercase.") | |
cases.add_argument("-l", "--lowercase", action="store_true", | |
help="Make every letter lowercase.") | |
cases.add_argument("-s", "--swapcase", action="store_true", | |
help="Make lowercase letters uppercase and" | |
" vice-versa.") | |
parser.add_argument("-h", "--help", action="help", | |
help="Print this help message.") | |
# Display a help message and exit if the program is run without arguments | |
if len(sys.argv) == 1: | |
parser.print_help() | |
sys.exit(1) | |
args = parser.parse_args() | |
return args | |
def case_to_title(input_string): | |
"""Capitalize principle words.""" | |
# Build a *simplified* list of non-principles | |
articles = ["a", "an", "the"] | |
conjunctions = ["and", "but", "or"] | |
prepositions = ["at", "by", "of"] | |
excludes = articles + conjunctions + prepositions | |
output_string = [] | |
for index, word in enumerate(input_string.split()): | |
# Always capitalize the first word | |
if index == 0: | |
output_string.append(word.capitalize()) | |
else: | |
lowercase = word.lower() | |
if lowercase in excludes: | |
output_string.append(lowercase) | |
else: | |
output_string.append(word.capitalize()) | |
return " ".join(output_string) | |
def process_string(args): | |
"""Call the function corresponding to the chosen case and print the | |
processed string. | |
""" | |
if sys.stdin.isatty(): | |
input_string = args.string | |
else: | |
input_string = sys.stdin.read() | |
if args.capitalize: | |
output_string = input_string.capitalize() | |
elif args.capwords: | |
output_string = string.capwords(input_string) | |
elif args.titlecase: | |
output_string = case_to_title(input_string) | |
elif args.uppercase: | |
output_string = input_string.upper() | |
elif args.lowercase: | |
output_string = input_string.lower() | |
elif args.swapcase: | |
output_string = input_string.swapcase() | |
print output_string | |
sys.exit(0) | |
def main(): | |
"""Get program arguments and process them.""" | |
args = get_args() | |
process_string(args) | |
if __name__ == "__main__": | |
main() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment