# Pass negative number as command line argument

**URL:** <https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129>\
**Category:** ArgumentParser\
**Created:** [September 6, 2022, 3:54pm UTC](https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129 "2022-09-06T15:54:00Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Todd21](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@Todd21](https://forums.swift.org/u/Todd21)\
**Post date:** [September 6, 2022, 3:54pm UTC](https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129/1 "2022-09-06T15:54:00Z")

</div>

Is there a way to pass a negative number as an option or command line argument to ArgumentParser?

For example, the code below generates an error when I try to pass a -5 as the optional value.

```swift
% ./parsetest2        
num = 3

% ./parsetest2 --num 5
num = 5

%./parsetest2 --num -5
Error: Missing value for '--num <num>'
Help: --num <num> Specify a number as an option
Usage: print-number [--num <num>]
  See 'print-number --help' for more information.

```

The code:

```swift
import Foundation
import ArgumentParser

struct PrintNumber: ParsableCommand {
    @Option(help: "Specify a number as an option")
    var num: Int = 3
    
    func run() {
        print("num = \(num)")
    }
}

PrintNumber.main()

```

---

<div class="post-metadata">

**Author:** ![Todd21](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@Todd21](https://forums.swift.org/u/Todd21)\
**Post date:** [September 6, 2022, 3:57pm UTC](https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129/2 "2022-09-06T15:57:06Z")

</div>

I have resolved it: Use the "=" sign

% ./parsetest2 --num=-5  
num = -5

---

<div class="post-metadata">

**Author:** ![Todd21](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@Todd21](https://forums.swift.org/u/Todd21)\
**Post date:** [September 6, 2022, 4:52pm UTC](https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129/3 "2022-09-06T16:52:36Z")

</div>

Small observation along the same line as a negative number for an option: in the Math example code, the sequence of numbers for the argument must all be positive integers.

```swift
% ./parsetest3 add 4 3 5
12

% ./parsetest3 add 4 3 -5
Error: Unknown option '-5'
Usage: math add [--hex-output] [<values> ...]
  See 'math add --help' for more information.

```

---

<div class="post-metadata">

**Author:** ![mayoff](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mayoff/32/613_2.png) [@mayoff](https://forums.swift.org/u/mayoff)\
**Post date:** [September 6, 2022, 6:19pm UTC](https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129/4 "2022-09-06T18:19:46Z")

</div>

Does this work?

```swift
./parsetest3 add -- 4 3 -5

```

---

<div class="post-metadata">

**Author:** ![MPLewis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mplewis/32/40019_2.png) [@MPLewis](https://forums.swift.org/u/MPLewis)\
**Post date:** [September 6, 2022, 6:59pm UTC](https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129/5 "2022-09-06T18:59:51Z")

</div>

To the best of my knowledge, in order to have negative numbers parsed in an array, you'll have to set [the parsing strategy to `.unconditionalRemaining`](https://apple.github.io/swift-argument-parser/documentation/argumentparser/argumentarrayparsingstrategy/unconditionalremaining):

```swift
...
@Argument(parsing: .unconditionalRemaining, ...)
var numbers: [Int]
...
```

---

<div class="post-metadata">

**Author:** ![phoneyDev](https://avatars.discourse-cdn.com/v4/letter/p/49beb7/32.png) [@phoneyDev](https://forums.swift.org/u/phoneyDev)\
**Post date:** [September 6, 2022, 8:18pm UTC](https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129/6 "2022-09-06T20:18:40Z")

</div>

Does quoting help?

```swift
./parsetest2 --num "-5"

```

---

<div class="post-metadata">

**Author:** ![Todd21](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@Todd21](https://forums.swift.org/u/Todd21)\
**Post date:** [September 6, 2022, 8:26pm UTC](https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129/7 "2022-09-06T20:26:24Z")

</div>

Yes! Adding this to my notes

```swift
% ./parsetest3 add -- 4 3 -5
2

```

---

<div class="post-metadata">

**Author:** ![Todd21](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@Todd21](https://forums.swift.org/u/Todd21)\
**Post date:** [September 6, 2022, 8:31pm UTC](https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129/8 "2022-09-06T20:31:16Z")

</div>

No. neither single nor double quotes work. The equal and the negative number (without spaces) works.

```swift
% ./parsetest2 --num "-5"
Error: Missing value for '--num <num>'
Help: --num <num> Specify a number as an option
Usage: print-number [--num <num>]

./parsetest2 --num=-5  
num = -5

```

---

<div class="post-metadata">

**Author:** ![Todd21](https://avatars.discourse-cdn.com/v4/letter/t/c6cbf5/32.png) [@Todd21](https://forums.swift.org/u/Todd21)\
**Post date:** [September 6, 2022, 8:36pm UTC](https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129/9 "2022-09-06T20:36:19Z")

</div>

This works too. Thanks!

```swift
@Argument(
        parsing: .unconditionalRemaining,
        help: "A group of integers to operate on.")
    var values: [Int] = []

```

and then executing the code

```swift
% ./parsetest3 add 3 4 -5
2

% ./parsetest3 add -5 -4 -3
-12

```

---

<div class="post-metadata">

**Author:** ![MPLewis](https://sea2.discourse-cdn.com/swift/user_avatar/forums.swift.org/mplewis/32/40019_2.png) [@MPLewis](https://forums.swift.org/u/MPLewis)\
**Post date:** [September 6, 2022, 8:54pm UTC](https://forums.swift.org/t/pass-negative-number-as-command-line-argument/60129/10 "2022-09-06T20:54:05Z")

</div>

Unfortunately, quotes are processed at the shell level, so no program ever even sees the quotes you write and an Argument Parser executable can't make any parsing decisions based on that.
