Live data from Hacker News

Swift: Announcing ArgumentParser

swift.org

11–20 of 27 posts

Re: Swift: Announcing ArgumentParser

#11

Can we please have a 'New Version', 'Old Version' comparison, side by side. If this is an improvement, why are you telling me instead of showing me? Personally, the problem is command line tools, not argument parsing. Why would anyone create a command line tool instead of a GUI that only lets you enter the correct set of arguments that go together, grouped into a coherent UI? The end result should be a json. We all k…

curl, kubectl, jq, ag, grep, git, psql are all tools that are far more useful as a CLI to me. That’s just the few I could come up with top of mind but there are many more.

Re: Swift: Announcing ArgumentParser

#12

    @Argument(help: "The highest value to pick.")
    var highValue: Int
    
    func validate() throws {
        guard highValue >= 1 else {
            throw ValidationError("'' must be at least 1.")
        }
    }
Not saying there's anything wrong with this. But I wrote a generic command line parsing library for C# some years back, and simple validators like min / max values were handled with attributes. The elegance was that limits were defined in exactly one place in the code, and error messages like that manually constructed above were synthesized automatically. (Of course you could override with your own validation function for bespoke cases)

Re: Swift: Announcing ArgumentParser

#13
If you haven't seen it, I consider the best-in-class argument parsing library to be Optparse-Applicative. https://github.com/pcapriotti/optparse-applicative

It provides an applicative interface, which it turns out is very precisely impedance-matched to the nature of command line arguments.

It also integrates nicely with e.g. https://hackage.haskell.org/package/optparse-generic to automatically derive command line parsers from data structures.

An example from a recent process of mine: https://github.com/wyager/zfs-backup/blob/master/src/Lib.hs . You can see what the generated help text looks like in the README.

The argument parser is automatically derived from the data structure describing the commands available in my program, using optparse-generic. Some extra type annotations on the command data structure provide the detailed document text.

Of course, you don't have to use the fancy auto-generation stuff if you don't want to. Writing optparse-applicative parsers by hand is very pleasant as well. Here is the argument parser code for my website's server: http://paste.best/p/22_rCSCen8M=

Re: Swift: Announcing ArgumentParser

#14

One of my very, very, very favorite things about Python is argparse. [1] Very few libraries creates its API so well as to transcend its origins (JodaTime comes to mind). * Node.js [2] * Java/JVM [3] * Go [4] * Lua [5] * C++ [6] [7] It appears there is no Swift port. That is unfortunate, because besides the basics (optionals, positions, defaults, short and log options, auto generated docs, subcommands, descriptions, r…

Argparse is fantastic. I recently had to shift some code from taking input from a csv to taking input from command line and I was dreading how much coding it'd involve. It took me 5 minutes of googling to come across argparse and another minute to find my exact use case written out in 5 lines of code.

The kicker is that once you define it and let it capture inputs, it outputs absolutely standard professional level error messages for bad parameters. I did not have to write any exception handling code at all. I could not believe that what I thought would take me several days was done in less than an hour.

Re: Swift: Announcing ArgumentParser

#15
I can see it now. Swift will be the first actually code-less language. Just @pile @up @all @necessary @magic @keywords, and your program is complete.

Seriously, instead of doing things actually worth mentioning as programming language development, they kill time with collecting cute magic tricks? Swift could have been interesting but featurism and mission creep make it a bag of complicated magic even worse than C++. At least C++ templates let me build my own magic. I have a strong suspicion that I will never use Swift voluntarily.

Re: Swift: Announcing ArgumentParser

#16

One of my very, very, very favorite things about Python is argparse. [1] Very few libraries creates its API so well as to transcend its origins (JodaTime comes to mind). * Node.js [2] * Java/JVM [3] * Go [4] * Lua [5] * C++ [6] [7] It appears there is no Swift port. That is unfortunate, because besides the basics (optionals, positions, defaults, short and log options, auto generated docs, subcommands, descriptions, r…

I can't stand argparse. It's like they tried their hardest to force you to be imperative and prevent composability. For example, instead of being able to define a parser for a subcommand and build a top-lever parser from that, you need to do this:

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers()
    a = subparsers.add_parser('a')
    a.add_argument('bar')
A better API would look more like this:

    Parser(Subparsers({
       'a': Parser(Arg('bar')),
    }))
On top of that it has a number of bugs in edge cases they refuse to document or fix. For example, you can't have a subcommand that always collects all the remaining arguments as-is. That's a pretty basic use-case when you want to pass those arguments to another program.

Re: Swift: Announcing ArgumentParser

#17

One of my very, very, very favorite things about Python is argparse. [1] Very few libraries creates its API so well as to transcend its origins (JodaTime comes to mind). * Node.js [2] * Java/JVM [3] * Go [4] * Lua [5] * C++ [6] [7] It appears there is no Swift port. That is unfortunate, because besides the basics (optionals, positions, defaults, short and log options, auto generated docs, subcommands, descriptions, r…

I can't stand argparse. It's like they tried their hardest to force you to be imperative and prevent composability. For example, instead of being able to define a parser for a subcommand and build a top-lever parser from that, you need to do this: parser = argparse.ArgumentParser() subparsers = parser.add_subparsers() a = subparsers.add_parser('a') a.add_argument('bar') A better API would look more like this: Parser(…

Python is indeed an imperative language, along with the others I listed. (Your proposal would indeed be at home in a functional language like LISP.)

But I reject the idea that imperative is bad. You can compose imperative programming fine.

    def my_parser(parser):
        parser.add_argument('bar')

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers()
    my_parser(subparsers.add_parser('a'))
    my_parser(subparsers.add_parser('a2'))
> you can't have a subcommand that always collects all the remaining arguments as-is.

    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers()
    a = subparsers.add_parser('a')
    a.add_argument('args', nargs='*')
That works just fine.

    ./example a arg1 arg2 arg3

    ./example a -- arg1 --look-ma-hyphens-arg2 arg3
If you're complaining about the double hyphen syntax....that's a universal convention for UNIX-ish CLI positional arguments. Otherwise things get really murky.

    ./example a --help
Did I want help on the `a` subcommand, or did I want to pass `--help` to the `a` subcommand? The `--` syntax lets you distinguish.

Re: Swift: Announcing ArgumentParser

#18

Can we please have a 'New Version', 'Old Version' comparison, side by side. If this is an improvement, why are you telling me instead of showing me? Personally, the problem is command line tools, not argument parsing. Why would anyone create a command line tool instead of a GUI that only lets you enter the correct set of arguments that go together, grouped into a coherent UI? The end result should be a json. We all k…

CLI tools exist for composibility.

How many lines non-ignored lines are in my git worktree?

   git ls-files -coz --exclude-standard | xargs -0 cat 2>/dev/null -- | wc -l --
How would you ever create a GUI with that much flexibility?

Re: Swift: Announcing ArgumentParser

#19

I can see it now. Swift will be the first actually code-less language. Just @pile @up @all @necessary @magic @keywords, and your program is complete. Seriously, instead of doing things actually worth mentioning as programming language development, they kill time with collecting cute magic tricks? Swift could have been interesting but featurism and mission creep make it a bag of complicated magic even worse than C++.…

I don’t understand this comment. There is nothing magic there, the @ is just syntax for using property wrappers, which you can write and document yourself, they are a language feature. The argument parser is just a swift library that uses this feature. In particular, @Argument is not a keyword.
Post reply on HN