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

Can not set negative integer value in command line #117

Closed
ydcool opened this issue Apr 8, 2021 · 10 comments
Closed

Can not set negative integer value in command line #117

ydcool opened this issue Apr 8, 2021 · 10 comments
Labels
bug Something isn't working

Comments

@ydcool
Copy link

ydcool commented Apr 8, 2021

Describe the bug
Can not set negative value in command line , it'll treated as a command line flag rather than a value, and the escape character does not work.

To Reproduce
Steps to reproduce the behavior:

#  dasel put int -p toml -f /etc/containerd/config.toml '.oom_score' '-900'
Error: unknown shorthand flag: '9' in -900

#  dasel put int -p toml -f /etc/containerd/config.toml '.oom_score' "\-900"
Error: could not parse int [\-900]: strconv.ParseInt: parsing "\\-900": invalid syntax
Usage:
  dasel put int -f <file> -s <selector> <value> [flags]

Flags:
  -h, --help   help for int

Global Flags:
  -f, --file string       The file to query.
  -m, --multiple          Select multiple results.
  -o, --out string        Output destination.
  -p, --parser string     Shorthand for -r FORMAT -w FORMAT.
  -r, --read string       The parser to use when reading.
  -s, --selector string   The selector to use when querying the data structure.
  -w, --write string      The parser to use when writing.

Error: could not parse int [\-900]: strconv.ParseInt: parsing "\\-900": invalid syntax

Desktop (please complete the following information):

  • OS: linux/ubuntu 18.06
  • Version: dasel version v1.11.0-2-gbe51ab2
@ydcool ydcool added the bug Something isn't working label Apr 8, 2021
@TomWright
Copy link
Owner

Thanks for raising this @ydcool.

I can confirm this is still an issue in the latest version: dasel version v1.13.6.

This is related to #101, and is caused by: https://github.com/spf13/pflag/blob/85dd5c8bc61cfa382fecd072378089d4e856579d/flag.go#L962

The code causing this is out of my control (without a PR at least) but in the short term I could introduce a workaround in the form of:

dasel put int -p toml -f /etc/containerd/config.toml' '.oom_score' 'n900'

This would mean that throughout dasel, when attempting to parse an int it would check to see if it starts with an n and convert that to a negative number.

I'm going to raise an issue regarding the above and see if I can get the root cause solved.

@TomWright
Copy link
Owner

The related issue is here: spf13/pflag#311

@ydcool
Copy link
Author

ydcool commented Apr 9, 2021

Nice, I'll also keep an eye on it to see if I can help.

@ydcool
Copy link
Author

ydcool commented Apr 9, 2021

I noticed that grep command can handle the escape charather, so it's also due to the upstream problem?

# cat /etc/containerd/config.toml | grep '-900'
Usage: grep [OPTION]... PATTERN [FILE]...
Try 'grep --help' for more information.

# cat /etc/containerd/config.toml | grep '\-900'
oom_score = -900

@TomWright
Copy link
Owner

Yes that's correct. Cobra and pflag control the input to dasel and they don't seem to arguments beginning with a dash very well.

@TomWright
Copy link
Owner

On a side note, it seems that escaping the dash does bypass the pflag validation issue, but then the resulting string of \-900 doesn't parse as a valid int.

Perhaps I could left trim any escape characters when parsing integers.

@TomWright
Copy link
Owner

I can't just deal with integers here - it's a wider issue.

echo '{"items": []}' | dasel put string -p json '.items.[]' '-thing'
Error: unknown shorthand flag: 't' in -thing

@TomWright
Copy link
Owner

Hey @ydcool,

Since this is a wider issue and I'm getting no response from the upstream supporter I've added a -v, --value flag that should function as a workaround:

echo '{"number":100}' | dasel put int -p json -v -200 '.number'

Or in your case:

dasel put int -p toml -f /etc/containerd/config.toml -v '-900' '.oom_score'

This is currently available on the development build which is accessible via:

  1. Installing from source:
go install github.com/tomwright/dasel/cmd/dasel@master
  1. Running from docker
echo '{"name": "Tom"}' | docker run -i --rm ghcr.io/tomwright/dasel:development put int -p json -v '-900' '.age'
{
  "age": -900,
  "name": "Tom"
}

@ydcool
Copy link
Author

ydcool commented Aug 16, 2021

-v, --value works well, it's still a nice solution : )

@TomWright
Copy link
Owner

This is now released under v1.20.0

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants