cxd4 / zs-flash

N64 flash RAM editor for "Legend of Zelda: Majora's Mask".
Creative Commons Zero v1.0 Universal
11 stars 3 forks source link

Negative numbers don't work. #1

Closed cxd4 closed 8 years ago

cxd4 commented 9 years ago

A command like zs filename.bin -M 1 -128 ... is meant to give Link -128 magic points but currently fails.

The problem is that my current Unix-based option parsing accounts for the variadic nature of the arguments (where behavior can sometimes be different if 1, 2, 3 or more [optional] arguments to the same option have been passed). For this reason, the "-128" is interpreted as a new option, -1, while the "-M 1" is stuck at only 2 arguments.

It is most unfortunate that the flag prefix for Unix command processing coincides also with the prefix for negative numbers in standard writing. Most people would not have this problem as I do simply because they implement option switches to have a fixed, guaranteed number of arguments to them, thus removing the need to search for an ending arg.

cxd4 commented 9 years ago

unfavorable solution 1

Stop using the traditional - prefix for command-line switches, and use something else like the Microsoft-traditional / prefix. Therefore, zs filename.bin /M 1 -128 instead of zs filename.bin -M 1 -128 would evade the issue.

cxd4 commented 9 years ago

unfavorable solution 2

Drop the support for variadic options. You can no longer choose whether to pass 1, 2, 3 or more arguments to a switch: It must be exactly the same fixed number of parameters every time.

cxd4 commented 9 years ago

unfavorable solution 3

Require extra delimiter string arguments to be specified on the command line.

Instead of zs filename.bin -M 1 -128 -m 0 user must say zs filename.bin -M 1 -128 -- -m 0

The run-time string parser can then be updated to require 2 - characters to mark the end.

I don't like this solution much because -- already means something else in traditional Unix environments, and if I pick something else like ;, # or : I have to also be cautious to avoid picking something that doesn't significantly alter the behavior of the shell script on the native environment.

cxd4 commented 9 years ago

unfavorable solution 4

Ignore the issue, and just make the user re-express negative numbers as hexadecimals.

On a platform where sizeof(long) == 4 octets, zs filename.bin -M 1 0xFFFFFF80

In which case, potentially if using atoi instead of strtol, 0xFFFFFF80 could yield the intended -128.

ExtremeDude2 commented 9 years ago

fuck the user, number 4 ftw

cxd4 commented 9 years ago

lolXD

cxd4 commented 8 years ago

I've decided on a solution other than the 4 unfavorable ones I thought of.

Only consider arguments as option switches or flags, if they can't be interpreted as numbers. So -f is a command switch, but -1 isn't since it could be a value (negative one) for a flag.

The resolution is as of this commit: https://github.com/cxd4/zs-flash/commit/9fef329320de607d68165fb33662fb049f59f3ef