Passing argument to bench execute

Hello, is it possible to call a function that expects arguments with bench execute?

@RWEMA_Aimable,

use, --args or --kargs options to pass the arguments

e.g. bench execute --args "arg1, arg2, .., argN" method_name

for more information please check, bench execute --help

Thanks, Makarand

5 Likes

OK thanks got it

In my case bench execute --args β€œarg1” was not working
instead I used bench execute --args β€œ{β€˜arg1’}” this worked for me.

3 Likes

Thank you. We should mention it in the docs.

This worked for me:

bench --site dev.erpnext.host  execute  --args "{'My Company Name'}" myApp.myApp.doctype.myDocType.myPythonModule.myMethod
erpdev@djst:~/frappe-bench-DJST$ tree -L 1 apps
apps
β”œβ”€β”€ erpnext
β”œβ”€β”€ frappe
└── myApp
    └── myApp
        └── doctype
            └── myDocType
                └── myPythonModule

Where myPythonModule is a file containing a method:

@frappe.whitelist()
def myMethod(company):
  msg = "Result :: {}".format("Ok")
  myLogger(msg)
  return { "returned": msg }

Output is:

{"returned": "Result :: Ok"}

Fun fact: unlike many bench commands it is not necessary to run it from the bench root directory

3 Likes

:confounded:
Grrrrr

… and since even that isn’t sufficiently obvious:

bench --site dev.erpnext.host execute \
     --args "{\"company\": 'My Company Name', \"item\": 'My Item'}" \
     myApp.myApp.doctype.myDocType.myPythonModule.myMethod
@frappe.whitelist()
def myMethod(company, item):
  msg = f"Company :: '{company}', Item :: '{item}'"
  return { "returned": msg }

Output:

{"returned": "Company :: 'My Company Name', Item :: 'My Item'"}

WHY?

Parameter order is not guaranteed!

If you have numerous arguments, like:

"{ 'doctype', 'txt', 'searchfield', 'start', 'page_len', 'filters' }"

Passed to :

@frappe.whitelist()
def myMethod(doctype, txt, searchfield, start, page_len, filters):
    print(f"doctype: {doctype}")
    print(f"txt: {txt}")
    print(f"searchfield: {searchfield}")
    print(f"start: {start}")
    print(f"page_len: {page_len}")
    print(f"filters: {filters}")

You will end up with:

doctype: searchfield
txt: start
searchfield: txt
start: page_len
page_len: filters
filters: doctype