Skip to content

Make ioc -m/--macros independent of -d/--database order#53

Open
jacquelinegarrahan wants to merge 1 commit into
epics-base:masterfrom
jacquelinegarrahan:fix/ioc-macros-arg-order
Open

Make ioc -m/--macros independent of -d/--database order#53
jacquelinegarrahan wants to merge 1 commit into
epics-base:masterfrom
jacquelinegarrahan:fix/ioc-macros-arg-order

Conversation

@jacquelinegarrahan

Copy link
Copy Markdown

Make ioc -m/--macros independent of -d/--database order

Branch: fix/ioc-macros-arg-ordermaster

Summary

src/python/epicscorelibs/ioc.py main() used a custom argparse action that
snapshotted ns.macros at the moment each -d was parsed:

class DbAction(argparse.Action):
    def __call__(self, parser, ns, values, opt):
        ns.database.append((values, ns.macros))

argparse processes arguments left-to-right, so the macros only took effect for
-d options that appeared after -m. That means:

-m macro=value -d db.db     # works: db loaded with macro=value
-d db.db -m macro=value     # BROKEN: db loaded with EMPTY macros

The broken order is exactly the usage documented in this module:

# Run with python -m epicscorelibs.ioc -d /path/to/db -m macro=value,macro2=value2

so the documented command silently loads the database with no macro
substitution.

Fix

Collect database paths with a plain action='append' and pair each with the
parsed args.macros after parsing, so a single -m applies to every -d
regardless of order:

parser.add_argument('-d', '--database', default=[], action='append', ...)
args = parser.parse_args()
dbs = [(database, args.macros) for database in args.database]
start_ioc(dbs=dbs, ...)

Verification

-d x.db -m a=b   ->  [('x.db', 'a=b')]     # documented order now works
-m a=b -d x.db   ->  [('x.db', 'a=b')]     # still works
-m a=b -d x.db -d y.db -> [('x.db','a=b'), ('y.db','a=b')]

ioc.py byte-compiles.

Note / trade-off

The previous per--d snapshotting could in principle assign different macros
to different databases via interleaved -m ... -d ... -m ... -d .... That
behavior was undocumented and worked only in one direction; this change makes a
single -m apply uniformly to all databases, matching the documented CLI. If
per-database macro sets are ever required, that should be a separate, explicit
feature (e.g. -d db=macros).

The custom DbAction snapshotted ns.macros at the moment each -d was
parsed, so "-d db -m macro=value" (exactly the order in this module's own
documented usage line) loaded the database with empty macros; only
"-m macro=value -d db" worked. Collect database paths with a plain
append action and pair them with the parsed macros after parsing, so the
macros apply to every database regardless of argument order.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
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.

1 participant