avrae / draconic

The Draconic language: a scripting language based off Python to run user scripts on the server safely.
MIT License
18 stars 7 forks source link

Argument Parsing has strange behaviour #32

Open ianfhunter opened 9 months ago

ianfhunter commented 9 months ago

If I want to provide an optional argument to my alias, for example !myalias debugmode, the logic inside draconic for parsing arguments is not very helpful.

If I call the following code as !bug hello and just !bug

!alias bug embed
-desc "Hi {{'%1%'}}

The first will print "Hi hello" and the latter "Hi %1%".

I would expect that when %1% is not provided, it would be replaced with an empty string, so we could do something like this:

!alias bug embed
-desc "Hi {{'%1%' if '%1%' != '' else ''}}

However, because it's not replaced, it's impossible to detect if the value isn't provided reliably

!alias bug embed
-desc "Hi {{'%1%' if '%1%' != '%1%' else ''}}

Will return "%1%" because both sides of the comparison are replaced.

The only workaround I can think of is something like:

!alias bug embed
-desc "Hi {{'%1%' if "%" not in '%1%' else ''}}

However, that would exclude valid inputs like "Hi 100%"

I do not know that the other argument parse types - "%*%", "&1&", etc work any better.

Proposed Solution: If I was writing this code, I would expect these to be replaced with None or "" on no argument received

Lathaon commented 9 months ago

A common solution has been this:

!alias bug embed
-desc "Hi {{"&1&" if "&1&" != "&"+"1&" else ""}}"

Personally I prefer to work with &ARGS&, as lists of arguments feel cleaner to work with in most cases.

(As a side note, I'd recommend visiting Avrae's Discord server rather than jumping straight to GitHub)