Skip to content

feature(bot): add CLI argument parsing helper#4

Open
davla wants to merge 9 commits intomasterfrom
feature/args
Open

feature(bot): add CLI argument parsing helper#4
davla wants to merge 9 commits intomasterfrom
feature/args

Conversation

@davla
Copy link
Collaborator

@davla davla commented Jun 1, 2025

Why

@Cruifer is starting to expect and rely on help texts in custom bot scripts. That poses a number of problems at present:

  • We don't always have a -help flag to show the script __doc__, even though we do have a docstring most of the times.

  • The actual parameter names and what's contained in the script can mismatch, as the whole argument parsing is implemented manually

  • Since we have to use with Pywikibot's terrible CLI option synthax (-name:<value>), in the few places where we have used the Python standard library we have inconsistency, as we have a sane syntax (e.g. --name <value>).

What

After looking around, I couldn't find any decent library that would allow CLI named parameters to be split on : and start with a single - (henceforth referred to as Pywikibot's terrible CLI option syntax). I hence had to implement something myself.

PcwCliArgs is yet another ArgumentParser-looking CLI argument parsing utility. You add options, flags and positional arguments, and then you parse them into a dictionary. If the -help flag is found, it prints an auto-generated help text and exits.

I wanted to do something with dataclasses and reflection, but apparently Python's class attributes can't have their own docstrings, so here we are with a builder interface.

The only issue is that we'd need the current directory in the PYTHONPATH environment variable. That's not hard to achieve though.

Copy link
Collaborator

@flavio-a flavio-a left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think I had some kind of objection last time I reviewed thiw but I can't remember it right now, so I'll approve before it comes back to mind.

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.

2 participants