apple / apple/swift-argument-parser

Passing context into parsed commands

Open
#254 2 comments 1 reaction 0 assignees View on GitHub
Dominant language
Swift
Stars
3.8k
Forks
411
Avg merge
7d 13h
Merged PRs (30d)
17

Description

I'd like to add some context into every command. Ideally, initialize it with objects.

Currently [`ParsableArguments`](https://github.com/apple/swift-argument-parser/blob/main/Sources/ArgumentParser/Parsable%20Types/ParsableArguments.swift#L30) requires `init()`. SAP uses it to instantiate commands once input has been parsed.

Is there a way to inject e.g. DI into parsed command? I can always create a singleton and then get it from command instance, but I'd love to `init(with di: MyDI)` instead. Also, I can parse `: ParsableArguments`, but I'd love to keep command tree feature available, and that works only with `ParsableCommand` and configurations as far as I can tell.

Contributor guide

Open the contributing guide

Research direction

Start with Sources/ArgumentParser/Parsable Types/ParsableArguments.swift, especially the init() requirement, and trace how SAP instantiates commands after parsing. Compare ParsableArguments parsing with ParsableCommand configurations and define an injection approach that preserves the command-tree feature; the issue is done when the supported behavior and initialization contract are established.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
cli
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.