Commit Graph
338 Commits
Author SHA1 Message Date
Nate Cook b64f05012e Add string tokenization with quoting 2023-10-08 16:26:53 -05:00
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 fee6933f37 Update changelog for 1.2.2 release (#549) 1.2.2 2023-02-09 09:25:51 -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 4ad606ba5d Update changelog for 1.2.1 release (#540) 1.2.1 2023-01-13 13:15:14 -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
Yim Lee 255ef66383 Change doc references (#532)
* Change doc references

Together with https://github.com/apple/swift-argument-parser/pull/531, updating GitHub pages references to those hosted by Swift Package Index.

* Change GitHub Pages reference to Swift Package Index
2023-01-09 11:17:53 -06:00
Nate Cook 9f103e4e68 Include more platforms in availability (#539) 2023-01-09 11:11:22 -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
Ashkan Ebtekari 83c0ef07c3 Improved Project Status Section In Readme (#530)
* Improved Project Status Section In Readme
2022-12-10 15:53:02 -06:00
Rauhul Varma 8f7e87bd0e Partially revert "Generate Manual" target name (#527)
- The name of "GenerateManualPlugin" was changed to "Generate Manual" to
  improve the plugin's display name in Xcode. This however results in
  the `.pluginWorkDirectory` provided by SPM to include a space (' ') in
  the path. The space in the output path by itself is not a deal
  breaker, but unfortunately `man` (at least on macOS) does not properly
  handle path arguments with spaces. Example:

  ```shell
  ➜  swift package generate-manual
  ...
  Generating manual for roll...
  Generated manual in '.build/plugins/Generate Manual/outputs/roll'
  ...
  ➜  man '.build/plugins/Generate Manual/outputs/roll/roll.1'
  /usr/bin/man: line 413: cd: .build/plugins/Generate: No such file or \
    directory
  ```

  IMO, easily previewing the generating manual takes precedence over the
  UI in Xcode; as a result, this commit partially reverts the plugin's
  name to "GenerateManual" to hopefully provide a good middle ground of
  an output path without a space and a display name in Xcode that is a
  little easier on the eyes than "GenerateManualPlugin" was.
2022-11-21 22:46:34 -08: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 e48e467b90 Fix deprecation warnings in unit tests (#526)
- Fixes deprecation warnings in unit tests by marking tests which cover
  deprecated functionality as @available(*, deprecated) themselves.
2022-11-21 22:24: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
Rob RussoandRob Russo 8f9fa6fc9f Reword Enums to Remove "Master" (#521)
Co-authored-by: Rob Russo <robert_russo@apple.com>
2022-11-15 12:30:40 -06:00
Kenta Kubo 208594a757 WASI: Fix Platform.exit (#520) 2022-11-14 09:42:12 -06:00
Nate Cook 10ee916bac Add .spi.yml for Swift Package Index doc support (#519) 2022-11-08 18:19:03 -06:00
Nate Cook fddd1c0039 Update changelog for 1.2.0 release (#518)
Update changelog for 1.2.0 release and
update README code sample for latest version

Resolves #500.
1.2.0
2022-11-08 13:45:58 -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
Clay Ellis 76259ebe8c Change name of plugin target to readable form (#505) 2022-10-18 12:50:55 -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
Nate Cook d88d4de38b Revert README code sample to latest release (#498) 2022-09-23 10:39:16 -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