Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add params to brownie run #398

Open
drLis opened this issue Apr 8, 2020 · 11 comments
Open

Add params to brownie run #398

drLis opened this issue Apr 8, 2020 · 11 comments
Labels
cli Relating to the Brownie command-line interface enhancement New feature or request

Comments

@drLis
Copy link

drLis commented Apr 8, 2020

Overview

Now we can put args and kwargs into brownie run, when we are using brownie console mode (here in code), but we can't do it through calling of brownie run from shell. Possible it's very useful feature in CI/CD, scripts etc

  • I want to deploy my token in CI/CD with dynamic params (for example address of other contracts, which have deployed earlier in CI/CD). Now I deploy their manually through brownie console.
  • I want to run my brownie scripts without interactive mode in docker files.

Specification

For example we can add args and kwargs so:
brownie run <filename> [<function>] [<args1>] … [<argsN>] [options] [kwargs], where
* [<args1>] … [<argsN>] - is tuple of args
*[kwars] - is kwargs, but without reserves keys like --network

Dependencies

I could not search.

If we can do these changes, than I can implement it at the next week.

@iamdefinitelyahuman
Copy link
Member

Thanks for this proposal! I agree with you on all points:

  • There is a valid use case
  • the run method already exposes this functionality
  • it won't break anything to add it to the CLI

If you are OK with implementing the functionality, I'm happy to add it to the next release 👍

@iamdefinitelyahuman iamdefinitelyahuman added cli Relating to the Brownie command-line interface enhancement New feature or request labels Apr 8, 2020
@davidfloyd91
Copy link

hey folks, came across this issue cause i was hoping to add command-line arguments to a brownie run call. doesn't look like it's been implemented but i wanted to make sure?

@drLis
Copy link
Author

drLis commented Jun 16, 2021

@davidfloyd91 yes, sorry, but I could not to find any time for this proposal implementation last year. I'm not python developer so can't develop it fast

@pjenness
Copy link

pjenness commented Oct 2, 2021

Hiya

Did this ever get resolved?

Im trying to run
brownie run './scripts/create_myToken.py --name "Myname"' --network rinkeby

I have args = sys.argv[1:] to catch --name etc like normal python

But brownie doesnt seem to like this command line format and returns error
"Usage: brownie run [] [options]"

Is there a solution ?

Cheers
-P

@gosuto-inzasheru
Copy link
Contributor

@poolpitako
Copy link
Contributor

@iamdefinitelyahuman close plz

@gosuto-inzasheru
Copy link
Contributor

yes, can confirm this works! thank you kindly for your work @wysenynja!

just note that args are always parsed as strings, so they might require some casting.

@ClaudeF4491
Copy link

ClaudeF4491 commented Mar 24, 2022

@jorijnsmit @wysenynja @pjenness I was wondering if you can provide some clarification on usage. Is it possible to have -- parameters, or optional arguments?

For example, I have this script with two required arguments (aaa, bbb) and one optional (ccc):

import argparse
from brownie import config, network

def run(aaa, bbb, ccc=None):
    print("in run")
    print(aaa)
    print(bbb)
    print(ccc)


if __name__ == "__main__":
    parser = argparse.ArgumentParser()

    # configure default parameters for event messaging
    parser.add_argument("--aaa", type=int, required=True, help="aaa")
    parser.add_argument("--bbb", type=str, required=True, help="bbb")
    parser.add_argument("--ccc", type=str, required=False, help="ccc (optional)")
    args = parser.parse_args()

    print("Inside main (never prints with `brownie run` call)", args)
    run(args.aaa, args.bbb, args.ccc)

If I run with python directly, i.e.

python3 scripts/sample.py --aaa=1 --bbb=2 --ccc=3

I get:

Inside main (never prints with `brownie run` call) Namespace(aaa=1, bbb='2', ccc='3')
in run
1
2
3

However, if I run with brownie run:

  1. It doesn't allow any of the -- entries, i.e. --aaa=1
  2. It only takes positional arguments

Example 1: No optional argument, works

$ brownie run scripts/sample.py run 1 2

Running 'scripts/sample.py::run'...
in run
1
2
None

Example 2: With optional argument, works

$ brownie run scripts/sample.py run 1 2 3

Running 'scripts/sample.py::run'...
in run
1
2
3

Example 3: With dashes, does NOT work:

$ brownie run scripts/sample.py run 1 2 --ccc=3
Brownie v1.18.1 - Python development framework for Ethereum

Usage: brownie run <filename> [<function>] [<arg>...] [options]

@ClaudeF4491
Copy link

ClaudeF4491 commented Mar 24, 2022

Ok, now digging a bit into the code. brownie run calls doctopt to parse out all the configs.
@iamdefinitelyahuman tagging you since I see you were the contributor to doctopt. Wondering if you have any ideas or ways to get kwargs to work with brownie commands.

It is right around here where it checks the parsed arguments:

matched, left, collected = pattern.fix().match(parsed_arg_vector)

When running the above code,

$ brownie run scripts/sample.py run 1 2 --ccc=3

I see this pattern defined:

Required(
    Required(
        Command('run', False), 
        Argument('<filename>', None), 
        NotRequired(
            Argument('<function>', None)
        ), 
        NotRequired(
            OneOrMore(
                Argument('<arg>', None)
            )
        ), 
        NotRequired(
            OptionsShortcut(
                Option(None, '--network', 1, None), 
                Option(None, '--silent', 0, False), 
                Option('-I', '--interactive', 0, False), 
                Option('-r', '--raise', 0, False), 
                Option('-g', '--gas', 0, False), 
                Option('-t', '--tb', 0, False), 
                Option('-h', '--help', 0, False)
           )
        )
    )
)

Notice OptionsShortcut. That's the only place where -- kwargs can be caught. As a result, the pattern.fix().match(parsed_arg_vector) sticks --ccc argument into the left variable.

matched

matched = True

left

left = [Option(None, '--ccc', 1, '3')]

collected

collected = [Command('run', True), Argument('<filename>', 'scripts/sample.py'), Argument('<function>', 'run'), Argument('<arg>', ['1', '2'])]
[Option(None, '--ccc', 1, '3')]
[Command('run', True), Argument('<filename>', 'scripts/sample.py'), Argument('<function>', 'run'), Argument('<arg>', ['1', '2'])]
n

The next line:

if matched and left == []:

sees left contains something (--ccc) and as a result bombs out with DocoptExit exception.

So given that, I'm not sure how to pass kwarg arguments in. I see others in this thread mention using sys.argv directly. But I'm not even sure how to do that, given it raises an exception before it even gets to my function.

@BlinkyStitt
Copy link
Collaborator

BlinkyStitt commented Mar 25, 2022 via email

@ClaudeF4491
Copy link

I use click for my extra parsing. I have it use \ as my prefix (instead of --) so that docopt doesn't try to parse them
Ah! Clever workaround. Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cli Relating to the Brownie command-line interface enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

8 participants