Commit Graph
240 Commits
Author SHA1 Message Date
Nate Cook a76f59f446 Use output streams when printing prompts
When deriving input from a non-interactive source, send the prompts
to a null output stream rather than to stdout.
2023-10-07 13:31:56 -05:00
Nate Cook 8efa5bc7b8 Change the timing of e.g. -h checks. 2023-02-20 18:01:46 -06:00
Nate Cook 2161ea4a6f Merge branch 'main' into feature/interactive 2023-02-09 22:54:44 -08:00
Nate Cook f674173291 Don't let .allUnrecognized consume built-in flags (#550)
Arguments declared with the `.allUnrecognized` parsing strategy
currently capture built-in flags, which isn't intended. This
fixes that issue by looking for built-in flags in the captured
portion of the input before decoding.

Fixes rdar://104990388
2023-02-08 21:02:46 -08:00
Nate Cook a9b9644153 Stop removing underscores from CodingKey names in InputKey (#548)
When a property wrapper is applied to a property, the property's
storage is given a name with a prefixed underscore. That is,
for a property named `x`, the actual storage is named `_x`.

That prefixed storage is what is visible through reflection, so
when building an ArgumentSet from a command type's Mirror, we
need to remove the leading underscore. This is done when creating
an InputKey for each property.

However, InputKeys are also created from CodingKeys during
decoding of a ParsableCommand. These CodingKeys _do not_ have
the leading underscore that is visible, so any underscores
that appear are actually from the declaration of the property
with an underscored name. Removing leading underscores from
CodingKey names results in a mismatch when trying to find
the decoded value.

This change simplifies the InputKey type to use an array
path instead of an indirect enum and removes the leading
underscore dropping when creating an InputKey from a CodingKey.

rdar://104928743
2023-02-02 08:33:51 -06:00
Nate Cook 478c2df6ab Improve failure message for backdeployed AsyncParsableCommand (#547)
When an executable with asynchronous commands is backdeployed,
the compiler chooses the synchronous `main()` unless a minimum
availability target is provided for the root command type. This
changes the error message provided when the incorrect `main()`
function is called to direct the tool's author to a correct
solution.
2023-01-27 10:45:06 -06:00
Nate Cook 846d1ad58c Provide type annotation for multi-line closure (#543) 2023-01-13 10:52:28 -06:00
Tiago Lopes 3b6f81459b Fix .postTerminator usage message (#542)
* Fix synopsis for .postTerminator parsing strategy

* Add test for .postTerminator usage message generation
2023-01-13 08:23:44 -06:00
matsuji 99e2e97fd9 Improve fish completion (#376, #534) (#535)
A generated fish script didn't complete an option after an argument.

The cause is the generated fish script doesn't accept an input text which already has arguments.
For example, when the input is `repeat -`, the script can complete `--count`, but when the text is `repeat foo -`, the script cannot complete `repeat foo --count`, because the input text already has the argument "foo".

To fix the issue, `FishCompletionsGenerator` got a capability that can accept a text which has arguments.
2023-01-09 10:55:41 -06:00
Rauhul Varma bc29743b72 Update tests with easier to read diff output (#529)
- Renames AssertEqualStringsIgnoringTrailingWhitespace to
  AssertEqualStrings and updates the implementation to require matching
  trailing whitespace.
- Updates AssertEqualStrings to include much easier to read diff output
  when CollectionDifference is available. This should add developers
  when tests fail by providing more clear errors.
2022-11-21 22:29:52 -08:00
Rauhul Varma e7f312e06e Cleanup Foundation usage (#528)
- Changes uses of import Foundation in ArgumentParser to only expose the
  symbols needed to avoid growing unintentional dependencies.
- Replaces direct usage of EXIT_FAILURE in MessageInfo with ExitCode,
  exposed as a result of the above change.
2022-11-21 22:27:42 -08:00
Rauhul Varma c5f71fd49f Fix argument-parser versions in Documentation (#525)
- Updates README.md and GettingStarted.md to reference
  swift-argument-parser from: 1.2.0. Additionally, adds a minimum
  supported swift version table to README.md to guide users on older
  toolchains.
2022-11-21 22:19:05 -08:00
Nate Cook f94e4b9951 Disfavor deprecated property wrapper inits (#522)
This change is to make sure that the non-optional generic parameter
type is chosen for @Argument and @Option properties that provide a
default value. Previously, an optional type might be chosen depending
on how the `help` parameter was spelled, due to strange overload resolution.
In particular, using the `.init` caused selection of the deprecated
optional overload:

    // Unexpected:
    // Infers `Value` as `Optional<AbsolutePath>`
    @Argument(help: .init("The path"))
    var path = AbsolutePath("/")

    // Expected:
    // Infers `Value` as `AbsolutePath`
    @Argument(help: "The path")
    var path = AbsolutePath("/")

This addresses the issue by marking the deprecated overloads as
disfavored. rdar://102383455
2022-11-16 09:30:27 -06:00
Kenta Kubo 208594a757 WASI: Fix Platform.exit (#520) 2022-11-14 09:42:12 -06:00
Nate Cook 5535a95838 Fix some Swift 5.6 compatibility issues (#516)
* Use existential CodingKey parameters consistently

Swift 5.7 supports implicit opening for existentials, so these
conversions from `CodingKey` parameters to pass to methods that
are generic over `CodingKey` work fine. Prior to Swift 5.7, however,
these don't compile, with the message that `CodingKey` doesn't conform
to itself.

* Bump the required Swift version for the count-lines test

The overload resolution for the `static func main()` in an `@main`
type still had issues in Swift 5.6, such that a package with a min.
platform below that which works for concurrency backdeployment doesn't
properly resolve the AsyncParsableCommand `main()` function. In
Swift 5.7, this is properly resolved, so just the availability on
the main type is sufficient.

This change just skips the test of `count-lines` prior to Swift 5.7,
so that we can maintain the open platform minimum for the package
as a whole.
2022-11-04 16:18:24 -05:00
Nate Cook 4256f88488 Document changing file name when adding @main (#514) 2022-11-01 14:13:03 -05:00
Nate Cook 90334086d9 Fix error in ExpressibleByArgument docs (#512) 2022-10-28 14:08:54 -05:00
David Rönnqvist 49e3214db9 Fix 6 unresolved symbol links in documentation. (#511)
Also, remove no longer needed @Comment information about these links.
2022-10-28 13:36:39 -05:00
Nate Cook 3469a65d52 Skip unsupported tests on Windows (#510) 2022-10-26 10:37:54 -05:00
Nate Cook f8b5e7127c Update DocC curation for newest symbols (#507) 2022-10-14 14:56:03 -05:00
Nate Cook 541f7c6e66 Fix post-merge issues 2022-10-13 22:41:03 -05:00
Saleem Abdulrasool b99e170f1c ArgumentParser: repair the build after #504 (#506)
There are references to functions from the Windows SDK rather than the C
library for the console handling.  Explicitly import the necessary types
and functions from the WinSDK module so that Windows can build once more
with the changes to sequester the platform specific code.
2022-10-12 12:45:51 -05:00
Nate Cook 3d6df7cf37 [NFC] Sequester platform-specific code (#504) 2022-10-10 17:50:20 -05:00
Nate Cook b92507afd3 Merge branch 'main' into feature/interactive
# Conflicts:
#	CHANGELOG.md
#	Tests/ArgumentParserEndToEndTests/DefaultSubcommandEndToEndTests.swift
#	Tests/ArgumentParserUnitTests/DumpHelpGenerationTests.swift
#	Tests/ArgumentParserUnitTests/NameSpecificationTests.swift
#	Tests/ArgumentParserUnitTests/ParsableArgumentsValidationTests.swift
2022-10-10 10:43:27 -05:00
Saleem Abdulrasool 8a38c7ca5c build: repair the ArgumentParser build on main (#503) 2022-10-09 11:12:09 -07:00
Nate Cook a8b48bc389 Add two new ArgumentArrayParsingStrategy options (#496)
This adds two new parsing options for argument arrays, and renames
`.unconditionalRemaining` to `.captureForPassthrough`.

- `.allUnrecognized` collects all the inputs that weren't used during
parsing. This essentially suppresses all "unrecognized flag/option"
and "unexpected argument" errors, and makes those extra inputs
available to the client.
- `.postTerminator` collects all inputs that follow the `--`
terminator, before trying to parse any other positional arguments.
This is a non-standard, but sometimes useful parsing strategy.
2022-10-07 16:18:52 -05:00
Nate Cook b80fb05f45 Add API for titling an option group (#492)
This change lets you provide a title for option groups, which is used
when generating the help screen. Titled option groups, when they exist,
are placed between the ARGUMENTS and OPTIONS section of the help.
Multiple option groups with the same title are coalesced into a single
group.

For example, this command declaration:

    struct Extras: ParsableArguments {
      @Flag(help: "Print extra output while processing.")
      var verbose: Bool = false

      @Flag(help: "Include details no one asked for.")
      var oversharing: Bool = false
    }

    @main
    struct Example: ParsableCommand {
      @OptionGroup(title: "Extras")
      var extras: Extras

      @Argument var name: String?
      @Option var title: String?
    }

yields this help screen:

    USAGE: example [--verbose] [--oversharing] [<name>] [--title <title>]

    ARGUMENTS:
      <name>

    EXTRAS:
      --verbose               Print extra output while processing.
      --oversharing           Include details no one asked for.

    OPTIONS:
      --title <title>
      -h, --help              Show help information.
2022-09-26 18:47:40 -05:00
David Peterson 587d26a2aa Fixes incorrect value copying when ParseArguments has the same field name+type as the ParseCommand (Issue #322) (#495)
* Contains fixes and test cases for #322
2022-09-22 09:43:48 -05:00
Nate Cook 774de9c409 Fix help display for non-String RawRepresentables (#494)
RawRepresentable types that have a non-String raw value are having
values displayed in the help screen by converting the RawRep value
into a string. However, these values are by default parsed by their
raw value, so we should use that for display instead.

This is accomplished by adding a defaultValueDescription
implementation for all ExpressibleByArgument-conforming RawValue
types, and then basing the allValues implementation on that.
This generalizes the existing overloads for String-based RawRep types,
while also allowing users who customize their ExpressibleByArgument
implementation to provide the correct help value for clients.
2022-09-20 14:25:03 -05:00
Tony Allevato 841b8538e4 Add an explicit return type to flatMap closure. (#490)
This restores the ability to build with versions of Swift prior to 5.7.
2022-09-17 12:28:40 -05:00
Nate Cook 7506042c65 Fix default display in help for EnumerableFlag and other types (#486)
In the help for flags like --prefix/--no-prefix, use the name of the
default flag instead of true/false. When EnumerableFlag types
have separate help strings, show the correct default flag name
on the default flag in the help.
2022-09-14 11:16:15 -05:00
Kth d76260e009 [GSoC] Interactive mode should only run in an interactive shell (#485) 2022-09-14 11:15:49 -05:00
Rauhul Varma 607021b737 Unify @Argument and @Option initialization paths (#477)
- Fixes #466.
- Adds initializers to ArgumentDefinition generic over a Container type.
  The Container type must conform to a new internal protocol
  ArgumentDefinitionContainer which describes functionality like default
  set of help options for the argument defined by the property wrapper,
  etc.
- Adds overloads for Optional @Arguments and @Options with default
  values which emit deprecation warning to guide users towards using the
  non-Optional versions.
2022-09-10 18:40:31 -05:00
Nate Cook 09106ba1ca Add a validation message for an invalid decoder (#487)
If a ParsableArguments type doesn't implement init(from:) correctly,
it isn't decodable by the parser. This improves the validation
failure message for such types.
2022-09-09 15:12:31 -05:00
Kth 2adfba8240 [GSoC] Dialog builder for interactive mode (#484)
* Create dialog builder
2022-09-08 01:36:54 -05:00
Kth d12f530121 [GSoC] Conversation for required @Flag (#470)
* Interact for required Flag
2022-09-06 12:51:10 -05:00
Nate Cook d7aa4401fe Declare Hashable conformance for ArgumentVisibility (#481)
In addition to being useful, this allows us to drop the `@testable`
annotation for the ArgumentParser import in ArgumentParserTestHelpers,
which resolves #463.
2022-09-01 15:23:45 -05:00
Nate Cook 0bac2cc0f1 Allow default nil values for optional properties (#480)
This adds underscored initializers that let library users add `= nil` to
declarations of optional `@Option` and `@Argument` properties. Previously,
default values have been available for properties of non-optional types
only.

These new initializers use `_OptionalNilComparisonType` as the wrapped
value parameter, so only a `nil` literal is acceptable in the default
value position. This avoids the problem of declaring an optional property
with a non-`nil` default, which ends up negating the purpose of an optional.
2022-08-30 22:23:15 -05:00
Ian 67fa3c00cd Fix the defaultValueDescription for string enums (#476)
defaultValueDescription is always the enum case name, but that's not a
valid argument when the enum value differs from the case name. Extend
ExpressibleByArgument to use rawValue for defaultValueDescription for
string enums.
2022-08-26 16:49:30 -07:00
Rauhul Varma f1c6afd175 Fixup help options for built-in flags (#474)
- Fixes a bug where built-in flags such as --help and --version were not
  properly marked with isOptional which resulted in them appearing in
  generated content (such as completion scripts and manuals) as required
  arguments.
2022-08-26 00:13:12 -07:00
Rauhul Varma e978a3831f Default to single page manuals (#472)
- Changes the generate-manual --single-page argument to --multi-page, so
  generate-manual with create a single manual page with all subcommand
  information by default instead of many distinct files.
2022-08-26 00:03:35 -07:00
Kth 34472ebbf7 [GSoC] Conversation for required @Option (#468)
* Add test for @Option & @OptionGroup
* Fix duplicate tests
* Add short name option tests
2022-08-24 16:00:26 -05:00
Kth aa00bf74fc [GSoC] Add API for disabling interactive mode (#464) 2022-08-15 23:59:50 -05:00
Kth 4ee4251764 [GSoC] Asking to retype invalid values (#460) 2022-07-28 10:02:15 +09:00
Kth eda9ea2dde [GSoC] Conversation for required @Argument 2 (#457)
* Fix array parsing as supplemental input

* Add test for `ExpressibleByArgument`

* Add options for `CaseIterable`

* Add test for `@Argument(transform:)`
2022-07-25 19:00:33 +09:00
Kth d1d0eb4baa [GSoC] Partial initialization for ParsableCommand (#451)
* Initial proof of concept
2022-07-14 22:53:20 -05:00
Kth 975b435592 Fix invalid link in docs (#440) 2022-06-23 13:06:58 -05:00
Kth c61fc0f7f0 Fix AsyncParseableCommand hierarchy (#436) 2022-06-13 12:50:43 -05:00
Rauhul Varma 48a799e04a Add experimental manual page generation (#332)
- Adds a swift package manager command plugin called
  GenerateManualPlugin. The plugin can be invoked from the command line
  using `swift package experimental-generate-manual`. The plugin is
  prefixed for now with "experimental-" to indicate it is not mature and
  may see breaking changes to its CLI and output in the future. The
  plugin can be can be used to generate a manual in MDoc syntax for any
  swift-argument-parser tool that can be executed via
  `tool --experimental-dump-info`.
- The plugin works by converting the `ToolInfoV0` structure from the
  `ArgumentParserToolInfo` library into MDoc AST nodes using a custom
  (SwiftUI-esk) result builder DSL. The MDoc AST is then lowered to a
  string and written to disk.
- The MDoc AST included is not general purpose and doesn't represent the
  true language exactly, so it is private to the underlying
  `generate-manual` tool. In the future it would be interesting to
  finish fleshing out this MDoc library and spin it out, however this is
  not a priority.
- Next steps include:
  - Improving the command line interface for the plugin.
  - Adding support for "extended discussions" to Commands and exposing
    this information in manuals.
  - Further improve the escaping logic to properly escape MDoc macros
    that might happen to appear in user's help strings.
  - Ingesting external content a-la swift-docc so the entire tool
    documentation does not need to be included in the binary itself.
  - Bug fixes and addressing developer/user feedback.

Built with love,
@rauhul
2022-06-06 11:09:34 -05:00
Luciano Almeida 78213f3192 Improving edit distance string extension (#446) 2022-06-04 22:26:16 -05:00