0k / shyaml

YAML for command line
BSD 2-Clause "Simplified" License
767 stars 57 forks source link

Exit code != 0 when key not found #17

Closed Grokzen closed 9 years ago

Grokzen commented 9 years ago

When trying to get a key from a data structure and there is a possiblity that the key do not exists, i will not get a exit code (1 for example) that the key was not found. Currently i can only use a DEFAULT value and check if the default value was returned or not and assume it was a error then.

But this could be bad becuase i have to invent my own default value that would not collide with the possible values that the key could have if it would exist. Say for example i choose DEFAULT=-1 but it would break if it would be allowed to have -1 as a value for my key if it was defined.

To have sys.exit(1) runned in case the key is not found or similar error would be good i think.

vaab commented 9 years ago

Hmm, I'm not sure to understand your case as since 8d22a885b1105dcc75074c51a7f96ba313fbb076 (pushed and release in beginning of january 2015) :

$ echo | shyaml get-value a
Error: invalid path 'a', can't query subvalue 'a' of a leaf (leaf value is None).
$ echo "$?"
1

So you are probably thinking of something else, may I ask you to give me am example of a command that would show better what happens and what you expect ?

Grokzen commented 9 years ago

Have you tried this?

echo | shyaml get-value a
echo "$?"
1
cat config.yml | shyaml get-value foobar
echo "$?"
0
vaab commented 9 years ago

Well, still no clue of what you are looking for, sorry... Try to avoid being cryptic, as we are both loosing time here. I need a full reproduceable scenario (the config.yml is missing for me to reproduce your case) AND I need to know what you are expecting to see.

Let's create a dummy config file with only a and b key:

$ cat <<EOF > /tmp/config.yml
a: 1
b: 2
EOF

Then ask for:

$ cat /tmp/config.yml | shyaml get-value foobar
Error: invalid path 'foobar', missing key 'foobar' in struct.
$ echo $?
1

I urge you to verify that you have the correct last version of shyaml, and/or to give me a complete run down to reproduce your issue as I did twice. For instance, here, you didn't share your config.yml (a dummy version like mine would suffice).

For now, you failed to clearly expose any issue, as it seems that shyaml behave exactly how you wanted to.

I'm scratching my head hard around this to figure what could happen on your side. Are you sure you didn't provide any variable AFTER foobar ?

For instance:

$ cat /tmp/config.yml | shyaml get-value foobar ""
$ echo "$?"
0

Or:

$ cat /tmp/config.yml | shyaml get-value foobar "$EMPTYVARIABLE"
$ echo "$?"
0

But these are valid use cases I want to keep.

You'll help me figure your issue by following my advice to create a valuable bug report.

Grokzen commented 9 years ago

I will check if i have a older version tomorrow and get back to you. You used the exact same steps i used in the top half of your reply and no i did not provide any other variables after my call when i used cat before shyaml.

Grokzen commented 9 years ago

It was using the older version of shyaml, after upgrading to the latest version it works as i expects and i get $? == 1 when the key is not found. I guess then the other issue #10 that is still open could be closed if it describes the same thing and the solution is to upgrade to the latest release. Thanks for the help :]

vaab commented 9 years ago

Thanks for your report and for pulling out this issue that should have been closed since a while. This was corrected. Happy to help !