Created
March 2, 2014 23:11
-
-
Save markrwilliams/9315399 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
import argparse | |
import parsley | |
def make_argparser(reqs): | |
argparser = argparse.ArgumentParser() | |
for req in reqs: | |
argparser.add_argument('--' + req) | |
return argparser | |
providers_g = ''' | |
args_ = -> argparser.parse_known_args()[0] | |
args :x = args_:a !(getattr(a, x, None)):r ?(r) -> r | |
kwargs :x = !(kwargs.get(x)):r ?(r) -> r | |
''' | |
providers = parsley.makeGrammar(providers_g, {}) | |
configure_g = ''' | |
address = (kwargs('address'):a -> a) | |
| (args('host'):h args('port'):p -> (h, int(p))) | |
''' | |
configure = parsley.makeGrammar(configure_g, {}, extends=providers) | |
def test(**kwargs): | |
configurer = configure('') | |
configurer._grammar.globals.update(kwargs=kwargs, | |
argparser=make_argparser(['host', | |
'port'])) | |
print configurer.address() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment