Pass named arguments in args field in py_binary

376 Views Asked by At

Consider I have a py_binary :

py_binary(
    name = "mock",
    srcs = ["mock.py"],
    args = [
        "$(location @mockref//mockpackage)",
    ],
    data = [
        "@mockref//:mockfile",
    ],
    deps = [
        requirement("somerequirement"),
    ],
)

If mock.py accept a command line argument named --somearg using argument parser how can I pass this argument through the args field in bazel file without passing the argument in bazel build command?

1

There are 1 best solutions below

0
On

The args attribute is only used with the bazel run command:

https://docs.bazel.build/versions/main/be/common-definitions.html#binary.args

To run a binary as part of a build, typically you'd use a genrule or a custom Starlark rule. Something like:

genrule(
  name = "gen_mock_output",
  outs = ["mock_output"],
  exec_tools = [":mock"],
  cmd = "$(location :mock) --output $@",
)

then you would build the output of the genrule:
bazel build mock_output

or use the output of the genrule as an input to another target