Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support passing a negative number as an argument #31

Open
alexito4 opened this issue Mar 1, 2020 · 6 comments
Open

Support passing a negative number as an argument #31

alexito4 opened this issue Mar 1, 2020 · 6 comments
Labels
enhancement New feature or request help wanted Extra attention is needed

Comments

@alexito4
Copy link

alexito4 commented Mar 1, 2020

Imagine a command like this

import ArgumentParser

struct Absolute: ParsableCommand {
    
    @Argument() var number: Int
    
    func run() throws {
        print(abs(number))
    }
}

Absolute.main()

you can run

command 5

and it works. but running

command -5

fails with Error: Missing expected argument '<number>' which makes sense.

I would have expected being able to pass the argument as "-5" but that doesn't work either.

Any advice on how to handle this?

@natecook1000
Copy link
Member

We don't have a workaround for this right now. For an option, you can specify the .unconditional parsing strategy, but arguments don't provide that capability. The changes in #29 will make it possible to add support for this, though.

@natecook1000 natecook1000 changed the title How to pass a negative number as an argument? Support passing a negative number as an argument Mar 23, 2020
@natecook1000 natecook1000 added enhancement New feature or request help wanted Extra attention is needed labels Mar 31, 2020
@fizker
Copy link

fizker commented Jun 13, 2020

This seems a common issue with CLI apps. ls -5 gives error ls: illegal option -- 5. The solution there is ls -- -5. That solution seems to also work for SAP v 0.1.0.

@weissi
Copy link
Member

weissi commented Mar 20, 2023

@fizker That's not quite true. ls does support -1 as value for arguments. Consider for example

$ ls -D -1 -la /var/empty/
total 0
drwxr-xr-x   2 root  sys      64 -1 .
drwxr-xr-x  36 root  wheel  1152 -1 ..

-D just selects the date/time format in the long (-l) output. And as you can see, it lists -1 as the time.

@fizker
Copy link

fizker commented Mar 20, 2023

@weissi I think you are confusing values for arguments with arguments. In your example, [-D format] is the argument you are triggering, not -D followed by -1. The parser can easily handle that case, because it is expecting (and indeed requiring) a value after -D, and thus will not attempt to parse it as an argument. Similarly, ls -D -l will not display the date at all, because it never enables long format.

@weissi
Copy link
Member

weissi commented Mar 20, 2023

@fizker oh shoot, you're right. Sorry I thought this issue is about allowing negative numbers as values for arguments (like -D -1) but actually it's about negative numbers as arguments themselves (like ping -6).

@fizker
Copy link

fizker commented Mar 21, 2023

@weissi No problem, it happens to everyone :). You are correct though in that ls has an argument -1:

     -1      (The numeric digit “one”.) Force output to be one entry per line.  This is the default when output is not to a terminal. ```

`ls` only fails for unknown arguments unless they are succeeding `--`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants