Skip to content

Commit

Permalink
Force conversion of all -i, -m, -t to the correct case
Browse files Browse the repository at this point in the history
  • Loading branch information
theotherjimmy committed Jul 7, 2016
1 parent 6fda53b commit 5052e97
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
7 changes: 3 additions & 4 deletions tools/options.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@
from argparse import ArgumentParser
from tools.toolchains import TOOLCHAINS
from tools.targets import TARGET_NAMES
from utils import argparse_uppercase_type, argparse_lowercase_hyphen_type, argparse_many

from utils import argparse_force_uppercase_type, argparse_lowercase_hyphen_type, argparse_many

def get_default_options_parser(add_clean=True, add_options=True):
parser = ArgumentParser()
Expand All @@ -31,12 +30,12 @@ def get_default_options_parser(add_clean=True, add_options=True):
parser.add_argument("-m", "--mcu",
help="build for the given MCU (%s)" % ', '.join(targetnames),
metavar="MCU",
type=argparse_many(argparse_uppercase_type(targetnames, "MCU")))
type=argparse_many(argparse_force_uppercase_type(targetnames, "MCU")))

parser.add_argument("-t", "--tool",
help="build using the given TOOLCHAIN (%s)" % ', '.join(toolchainlist),
metavar="TOOLCHAIN",
type=argparse_many(argparse_uppercase_type(toolchainlist, "toolchain")))
type=argparse_many(argparse_force_uppercase_type(toolchainlist, "toolchain")))

if add_clean:
parser.add_argument("-c", "--clean", action="store_true", default=False,
Expand Down
5 changes: 3 additions & 2 deletions tools/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
from tools.targets import TARGET_NAMES
from tools.libraries import LIBRARIES
from utils import argparse_lowercase_type, argparse_uppercase_type, argparse_filestring_type, argparse_many
from utils import argparse_force_lowercase_type, argparse_force_uppercase_type



Expand All @@ -32,14 +33,14 @@
metavar="MCU",
default='LPC1768',
required=True,
type=argparse_many(argparse_uppercase_type(targetnames, "MCU")),
type=argparse_many(argparse_force_uppercase_type(targetnames, "MCU")),
help="generate project for the given MCU (%s)"% ', '.join(targetnames))

parser.add_argument("-i",
dest="ide",
default='uvision',
required=True,
type=argparse_many(argparse_lowercase_type(toolchainlist, "toolchain")),
type=argparse_many(argparse_force_lowercase_type(toolchainlist, "toolchain")),
help="The target IDE: %s"% str(toolchainlist))

parser.add_argument("-c", "--clean",
Expand Down
21 changes: 21 additions & 0 deletions tools/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,27 @@ def parse_type(string):
argparse_uppercase_hyphen_type = argparse_type(str.upper, True)
argparse_lowercase_hyphen_type = argparse_type(str.lower, True)

def argparse_force_type(case):
def middle(list, type_name):
# validate that an argument passed in (as string) is a member of the list of possible
# arguments after converting it's case. Offer a suggestion if the hyphens/underscores
# do not match the expected style of the argument.
def parse_type(string):
string = case(string)
newstring = string.replace("-","_")
if string in list:
return string
elif string not in list and newstring in list:
raise argparse.ArgumentTypeError("{0} is not a supported {1}. Did you mean {2}?".format(string, type_name, newstring))
else:
raise argparse.ArgumentTypeError("{0} is not a supported {1}. Supported {1}s are:\n{2}".format(string, type_name, columnate(list)))
return parse_type
return middle

# these two types convert the case of their arguments _before_ validation
argparse_force_uppercase_type = argparse_force_type(str.upper)
argparse_force_lowercase_type = argparse_force_type(str.lower)

# An argument parser combinator that takes in an argument parser and creates a new parser that
# accepts a comma separated list of the same thing.
def argparse_many(fn):
Expand Down

0 comments on commit 5052e97

Please sign in to comment.