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

WIP: split the argparser logic to one more level #837

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

tacaswell
Copy link
Collaborator

Currently we have two levels to the factory function to generate the CLI interface for caproto IOCs

  • ioc_arg_parser -> all-in-one that gives you two dictionaries
  • template_arg_parser -> gives you a parser and a function to extract those dictionaries

The first is useful for reducing boiler plate in servers, the second is useful if you need to inject some additional parameters to be extracted from the CLI.

This adds another layer which giver a ArgParser will add all of the standard flags which is useful when you have an existing CLI tool that you want to add caporto features to.

Currently we have two levels to the factory function to generate the CLI
interface for caproto IOCs

- ioc_arg_parser -> all-in-one that gives you two dictionaries
- template_arg_parser -> gives you a parser and a function to extract those
  dictionaries

The first is useful for reducing boiler plate in servers, the second is useful
if you need to inject some additional parameters to be extracted from the CLI.

This adds another layer which giver a ArgParser will add all of the standard
flags which is useful when you have an existing CLI tool that you want to add
caporto features to.
@wigging
Copy link

wigging commented Feb 12, 2024

Can you provide a basic example of how these new features are implemented by a user? How would template_arg_parser be used for the example shown below?

from caproto.server import PVGroup, ioc_arg_parser, pvproperty, run

class SimpleIOC(PVGroup):
    A = pvproperty(value=1, doc="An integer")
    B = pvproperty(value=2.0, doc="A float")
    C = pvproperty(value=[1, 2, 3], doc="An array of integers (max length 3)")

if __name__ == "__main__":
    ioc_options, run_options = ioc_arg_parser(default_prefix="simple:", desc="Example")
    ioc = SimpleIOC(**ioc_options)
    run(ioc.pvdb, **run_options)

@wigging
Copy link

wigging commented Feb 12, 2024

Adding a log level parameter to the run function would be useful for situations when not using the ioc_arg_parser approach.

from caproto.server import PVGroup, pvproperty, run

class SimpleIOC(PVGroup):
    A = pvproperty(value=1, doc="An integer")
    B = pvproperty(value=2.0, doc="A float")
    C = pvproperty(value=[1, 2, 3], doc="An array of integers (max length 3)")

if __name__ == "__main__":
    ioc = SimpleIOC(prefix="simple")
    run(ioc.pvdb, loglevel="info")

@tacaswell
Copy link
Collaborator Author

if __name__ == "__main__":
parser, split_args = template_arg_parser(
default_prefix="chirp:",
desc=textwrap.dedent(Chirp.__doc__),
)
def in_range(v):
v = float(v)
if not (0 < v < 1):
raise argparse.ArgumentTypeError(f" {v} not in range [0, 1]")
return v
parser.add_argument(
"--ramprate",
help="The multiplicative factor to apply when chirping.",
type=in_range,
default=0.75,
)
args = parser.parse_args()
ioc_options, run_options = split_args(args)
ioc = Chirp(ramp_rate=args.ramprate, **ioc_options)
run(ioc.pvdb, **run_options)
is one of a couple of existing examples of using template_arg_parser.

To use the new one it would be something like

...  # other code
parser = ...  # where ever you are getting it from now
split_function = extend_arg_parser(parser)  # mutates in place!
... # other code
args = parser.parse_args()
... # other code
ioc_options, run_options = split_args(args)
ioc = SimpleIOC(**ioc_options)
run(ioc.pvdb, **run_options)

@tacaswell
Copy link
Collaborator Author

I do not think we want to cross the details of setting up loggers and log levels with the business of actually running the IOC.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants