terumi-project / Terumi

Terumi - Shell scripts, reinvented
MIT License
4 stars 0 forks source link

[REQ] Support arguments in the main method #13

Open monoclex opened 4 years ago

monoclex commented 4 years ago

Feature description:

The main method in a Terumi application should have a way to get the arguments of the program. Types such as string, and classes should be accepted as the first argument in a main method:

main(string)
main(MyType)

Suggested implementation:

Windows and Linux are vastly different. See this extremely useful guide on how command line argument parsing is done.

For the time being (primarily because we don't have arrays), argv will not be supported. On linux, argv will get joined by tabs (to help differentiate them from regular arguments spaces, but keeping them as whitespace.) On windows, GetCommandLine will be used.

Thus, as we're dealing with only strings currently, main can accept a single parameter: string or a type that has a parse_args_string(string) function, with an empty constructor (the empty constructor can be private _ctor). The following should work:

main(string args)
{
    @println("Got args: '{args}'")
}

Would print out with the args "--hello-world" assuming the script being run is some/dir/a.exe (a.exe could be a powershell script, doesn't matter):

Got args: 'some/dir/a.exe --hello-world'

And for a class example:

class StringArgs
{
    string string_args

    // empty constructor
    ctor() {
    }

    // parse_string method
    parse_args_string(string args) {
        @println("Parsing string '{args'}!")
        string_args = args
    }
}

main(StringArgs args)
{
    @println("String args are: {args.string_args}")
}

Would print out with the args "--hello-world" assuming the script being run is some/dir/a.exe (a.exe could be a powershell script, doesn't matter):

Parsing string 'some/dir/a.exe --hello-world'!
String args are: some/dir/a.exe --hello-world

In the future, main should support a string array as an argument and for a type that is specified in main, it will need to have a parse_args_string_array(string[]) as well.