Skip to content

Conversation

@bkhouri
Copy link
Contributor

@bkhouri bkhouri commented Nov 21, 2025

There is a need to have an argument act as a flag or as an option. Introduce a new FlagOption type that support this.

Fixes: #829

Checklist

  • I've added at least one test that validates that my change is working, if appropriate
  • I've followed the code style of the rest of the project
  • I've read the Contribution Guidelines
  • I've updated the documentation if necessary

@bkhouri bkhouri force-pushed the t/main/gh829_add_new_argument_type_FlagOption branch from b73e9b0 to d08351c Compare November 21, 2025 02:57
Add a new FlagOption type that can be used as a flag or as an option.

Fixes: #829
@bkhouri bkhouri force-pushed the t/main/gh829_add_new_argument_type_FlagOption branch from d08351c to 61a6602 Compare November 21, 2025 03:08
Copy link
Contributor

@rauhul rauhul left a comment

Choose a reason for hiding this comment

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

  • xctest instead of swift-testing to support swift 5.7
  • synopsis: [--log-level [log-level]] or [--log-level [<log-level>]]
  • dump-help snapshot tests
  • completion snapshot tests
  • manpage support
  • manpage snapshot tests
  • documentation

@rgoldberg
Copy link
Contributor

rgoldberg commented Nov 21, 2025

Would it make more sense instead to provide this functionality via Option? Like:

@Option(parsing: .scanningForValue(default: "text"))
var showBinPath: String?

You'd add/modify the following (I haven't checked the code for proper generics/types/compilation/keywords/etc.; it's just pseudo-code to illustrate the concept. I also haven't checked to see what problems adding the new SingleValueParsingStrategy to Option might cause):

public struct SingleValueParsingStrategy: Hashable {
  
  // This is an addition
  public static func scanningForValue<Value>(default: Value) -> SingleValueParsingStrategy {
    self.init(base: .scanningForValue(default: default))
  }
  
}

struct ArgumentDefinition {enum ParsingStrategy {
    …
    // This is a modification
    case scanningForValue(default: Any? = nil)
    
  }
  
}

One possible issue: must ensure that the option terminator -- ensures that json in the following command line is interpreted as positional argument 1, not as a value for --show-bin-path, but that problem might exist for this any FlagOption solution, too:

cmd --show-bin-path -- json

@bkhouri
Copy link
Contributor Author

bkhouri commented Nov 21, 2025

Thanks for the feedback @rgoldberg . What you proposed makes more sense than what I implemented.

@bkhouri bkhouri marked this pull request as draft November 21, 2025 17:29
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.

Support an argument as a flag or option

4 participants