Skip to content

Commit

Permalink
added CreditParser and switched appropriate arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
tKe committed Apr 3, 2015
1 parent be2af91 commit be57302
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 8 deletions.
19 changes: 19 additions & 0 deletions commands/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,22 @@ def __call__(self, parser, namespace, values, option_string=None):
parser.format_help()
)

class CreditParser(int):
"""
argparse type helper for handling suffixes for thousand, million and "billion"
"""

suffixes = dict(zip("kmb", map(lambda x: 10**x, (3, 6, 9)))) # damn american billion. What's wrong with milliard?

def __new__(cls, val, **kwargs):
if isinstance(val, str):
if val[-1] in CreditParser.suffixes:
val = int(float(val[:-1]) * CreditParser.suffixes[val[-1]])
return super(CreditParser, cls).__new__(cls, val, **kwargs)

@classmethod
def register(cls, target):
target.register('type', 'credits', CreditParser)

def addArguments(group, options, required, topGroup=None):
"""
Expand All @@ -57,6 +73,7 @@ def addArguments(group, options, required, topGroup=None):
for option in options:
if isinstance(option, parsing.MutuallyExclusiveGroup):
exGrp = (topGroup or group).add_mutually_exclusive_group()
CreditParser.register(exGrp)
addArguments(exGrp, option.arguments, required, topGroup=group)
else:
assert not required in option.kwargs
Expand Down Expand Up @@ -170,13 +187,15 @@ def error(self, message):
)
)
parser.set_defaults(_editing=False)
CreditParser.register(parser)

subParsers = parser.add_subparsers(title='Command Options')
subParser = subParsers.add_parser(cmdModule.name,
help=cmdModule.help,
add_help=False,
epilog=cmdModule.epilog,
)
CreditParser.register(subParser)

arguments = cmdModule.arguments
if arguments:
Expand Down
4 changes: 2 additions & 2 deletions commands/buy_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,14 +84,14 @@
help='Limit to prices above Ncr',
metavar='N',
dest='gt',
type=int,
type="credits",
),
ParseArgument(
'--lt',
help='Limit to prices below Ncr',
metavar='N',
dest='lt',
type=int,
type="credits",
),
]

Expand Down
8 changes: 4 additions & 4 deletions commands/run_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
ParseArgument('--credits',
help='Starting credits.',
metavar='CR',
type=int,
type="credits",
),
]

Expand Down Expand Up @@ -150,13 +150,13 @@
ParseArgument('--gain-per-ton', '--gpt',
help='Specify the minimum gain per ton of cargo',
dest='minGainPerTon',
type=int,
type="credits",
default=1
),
ParseArgument('--max-gain-per-ton', '--mgpt',
help='Specify the maximum gain per ton of cargo',
dest='maxGainPerTon',
type=int,
type="credits",
default=10000
),
ParseArgument('--unique',
Expand All @@ -176,7 +176,7 @@
help='Reserve at least this many credits to cover insurance.',
default=0,
metavar='CR',
type=int,
type="credits",
),
ParseArgument('--routes',
help='Maximum number of routes to show. DEFAULT: 1',
Expand Down
4 changes: 2 additions & 2 deletions commands/sell_cmd.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,13 +47,13 @@
help='Limit to prices above Ncr',
metavar='N',
dest='gt',
type=int,
type="credits",
),
ParseArgument('--lt',
help='Limit to prices below Ncr',
metavar='N',
dest='lt',
type=int,
type="credits",
),
]

Expand Down

0 comments on commit be57302

Please sign in to comment.