Skip to content

Commit 5052e97

Browse files
committed
Force conversion of all -i, -m, -t to the correct case
1 parent 6fda53b commit 5052e97

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

tools/options.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@
1717
from argparse import ArgumentParser
1818
from tools.toolchains import TOOLCHAINS
1919
from tools.targets import TARGET_NAMES
20-
from utils import argparse_uppercase_type, argparse_lowercase_hyphen_type, argparse_many
21-
20+
from utils import argparse_force_uppercase_type, argparse_lowercase_hyphen_type, argparse_many
2221

2322
def get_default_options_parser(add_clean=True, add_options=True):
2423
parser = ArgumentParser()
@@ -31,12 +30,12 @@ def get_default_options_parser(add_clean=True, add_options=True):
3130
parser.add_argument("-m", "--mcu",
3231
help="build for the given MCU (%s)" % ', '.join(targetnames),
3332
metavar="MCU",
34-
type=argparse_many(argparse_uppercase_type(targetnames, "MCU")))
33+
type=argparse_many(argparse_force_uppercase_type(targetnames, "MCU")))
3534

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

4140
if add_clean:
4241
parser.add_argument("-c", "--clean", action="store_true", default=False,

tools/project.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
from tools.targets import TARGET_NAMES
1717
from tools.libraries import LIBRARIES
1818
from utils import argparse_lowercase_type, argparse_uppercase_type, argparse_filestring_type, argparse_many
19+
from utils import argparse_force_lowercase_type, argparse_force_uppercase_type
1920

2021

2122

@@ -32,14 +33,14 @@
3233
metavar="MCU",
3334
default='LPC1768',
3435
required=True,
35-
type=argparse_many(argparse_uppercase_type(targetnames, "MCU")),
36+
type=argparse_many(argparse_force_uppercase_type(targetnames, "MCU")),
3637
help="generate project for the given MCU (%s)"% ', '.join(targetnames))
3738

3839
parser.add_argument("-i",
3940
dest="ide",
4041
default='uvision',
4142
required=True,
42-
type=argparse_many(argparse_lowercase_type(toolchainlist, "toolchain")),
43+
type=argparse_many(argparse_force_lowercase_type(toolchainlist, "toolchain")),
4344
help="The target IDE: %s"% str(toolchainlist))
4445

4546
parser.add_argument("-c", "--clean",

tools/utils.py

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,27 @@ def parse_type(string):
223223
argparse_uppercase_hyphen_type = argparse_type(str.upper, True)
224224
argparse_lowercase_hyphen_type = argparse_type(str.lower, True)
225225

226+
def argparse_force_type(case):
227+
def middle(list, type_name):
228+
# validate that an argument passed in (as string) is a member of the list of possible
229+
# arguments after converting it's case. Offer a suggestion if the hyphens/underscores
230+
# do not match the expected style of the argument.
231+
def parse_type(string):
232+
string = case(string)
233+
newstring = string.replace("-","_")
234+
if string in list:
235+
return string
236+
elif string not in list and newstring in list:
237+
raise argparse.ArgumentTypeError("{0} is not a supported {1}. Did you mean {2}?".format(string, type_name, newstring))
238+
else:
239+
raise argparse.ArgumentTypeError("{0} is not a supported {1}. Supported {1}s are:\n{2}".format(string, type_name, columnate(list)))
240+
return parse_type
241+
return middle
242+
243+
# these two types convert the case of their arguments _before_ validation
244+
argparse_force_uppercase_type = argparse_force_type(str.upper)
245+
argparse_force_lowercase_type = argparse_force_type(str.lower)
246+
226247
# An argument parser combinator that takes in an argument parser and creates a new parser that
227248
# accepts a comma separated list of the same thing.
228249
def argparse_many(fn):

0 commit comments

Comments
 (0)