[Cuis-dev] [IMPROV] Configurable command line exception handling

Gerald Klix cuis.01 at klix.ch
Thu Jan 12 12:01:30 PST 2023


Hi Juan,

let me answer questions one at time (even those that were not asked:)

-q) If want to write small Cuis scripts, like the prototypical “Hello 
World” program, you would write  something like this:

squeak  -vm-display-null -headless Cuis6.0-5626 -d "StdIOWriteStream 
stdout nextPutAll: 'Hello World!'; newLine. Smalltalk quit"

If you want to dump all unknown contributors to a file; it would look 
like this:

squeak  -vm-display-null -headless Cuis6.0-5626 -d  "Smalltalk 
unknownContributors printOn: StdIOWriteStream stdout. Smalltalk quit" > 
unknownContributors.txt

Of course all these one-liners will end with `Smalltalk quit`, therefore 
I came up with a shorter way to achieve the same effect:

squeak  -vm-display-null -headless Cuis6.0-5626 -d  "Smalltalk 
unknownContributors printOn: StdIOWriteStream stdout" -q > 
unknownContributors.txt

-c) is completely useless, because the old implementation only accepts 
one `-s` option.
`-s` consumes and copies all the remaining arguments to 
`startUpScriptArguments`,
terminating the loop,I did not catch that detail.
I will fix this in the next version of the change.

--) Posix requires a program to stop option parsing once it encounters a 
double dash.
To give an example that proves the necessity of this behavior, just try 
this in your Mac's terminal:

echo Hehehe > -f

Than try to delete the file named “-f” using `rm -f`. This does not 
work, you have to use `rm -- -f`.
You can also check git, grep, ls or any other little Unix utility . Just 
check:

echo Hehehe > -r
ls -r

vs.

ls -- -r

(I leave it as an exercise to the gentle reader to get rid of that '-r' 
file.)

Therefore handling `--` should be done correctly, because it avoids 
unnecessary surprises.
But it also makes it easy to pass arguments to code package (classes) 
that are
installed with "-r". This part of Haver's installation package could be 
written in a less hacky way:

https://hg.sr.ht/~cy-de-fect/HaverOnCuis/browse/haver/Haverize.pck.st?rev=tip#L131

The same is true for the `-d` option, you can implement `echo` with Cuis 
like this:

squeak -vm-display-null -headless Cuis6.0-5626 -d  "Smalltalk 
startUpScriptArguments do: [ :arg | StdIOWriteStream stdout nextPutAll: 
arg; space]. StdIOWriteStream stdout newLine" -q -- -n 1 2 3 a b dee

compare to

echo -- -n 1 2 3 a b dee

Of course you want '--' handled correctly in your Cuis scripts, there is 
no use
in every author implementing it again, when she needs it.


#copyStartupScriptArgumentsFrom:)

Simply creates a fresh copy of the arguments after `--` for
each script, code file, package or piece of code passed on the command line.

There is a reason for recreating the array for each script, code file etc.:
I never like the Unix's getopt(3) behavior.
«By default, getopt() permutes the contents of argv as it scans,
so that eventually all the nonoptions are at the end.»
(https://www.man7.org/linux/man-pages/man3/getopt.3.html)
Option parsing and program should no mess with the command line arguments,
this creates all sorts of nasty side effects.

I hope I made my point about "end of options" and "--" clear.
Please give some time to come up with a simpler version of the change.


HTH,

Gerald


On 1/12/23 3:59 PM, Juan Vuletich wrote:
> Hi Gerald,
>
> I agree that "do not ignore exceptions" doesn't need an argument. 
> Then, maybe -e should never take an argument. I also like  -h and -v. 
> Not sure about the value of -q, but I wouldn't mind including it.
>
> But can you explain -- and #copyStartupScriptArgumentsFrom: ? Can you 
> give an example of use? I don't get it.
>
> Thanks,
>
> On 1/11/2023 6:53 PM, Gerald Klix via Cuis-dev wrote:
>> Hi guys,
>>
>> thanks a lot for the encouraging feedback.
>>
>> It motivated me to to improve the exception
>> ignoring stuff a bit; I just wanted '-e' to work without an argument.
>> Then I noticed, that we can't handle options without arguments.
>> Therefore I added, em well, some other options to the command line
>> parsing code. I hope I haven't overdone it.
>>
>> You can now say `squeak Cuis.image -h` and get this output:
>>
>> --- snip --
>> Command line options:
>> -h Display this help message and quit
>> -v Display the system information, containg the version and quit
>> -q Just quit
>> -e <exception> Evaluate exception and ignore it
>> -e Set the exception to ignore to nil, i.e. do not ignore exceptions
>> -r <feature> Require <feature>
>> -d <code> Evaluate <code>
>> -l <codeFile> Load e.g. file in <codeFile>
>> -s <scriptFile> Evaluate code ein <scriptFile>
>> -c Clear the array of start up script arguments
>> -- end of options; stop processing options
>>
>> Arguments for scripts, code files, packages (and <code>):
>> -s and -- store the remaining arguments in 'Smalltalk 
>> startUpScriptArguments'
>> -c clears the aforementioned array
>> Packages (-f), code files (-l) and code (-d) gets a fresh copy of the 
>> array if -- is present
>>
>> Multiple options:
>> Options -e, -r, -d, -l, -s and -c can be passed multiple times
>> --- snap ---
>>
>> Please review the change set and give me feedback.
>> There might be some incompatibilities.
>>
>>
>> HTH and Best Regards,
>>
>> Gerald
>>
>>
>>
>> On 1/11/23 2:33 PM, Dr. Nicola Mingotti wrote:
>>> nice !
>>>
>>> Nicola
>>>
>>>
>>>
>>>
>>>
>>>
>>>
>>> On 1/8/23 17:21, Gerald Klix via Cuis-dev wrote:
>>>> Hi all, Hi Juan,
>>>>
>>>> I was bit annoyed by the impossibility to debug scripts and packages
>>>> run or installed from the command line.
>>>> Therefore I added an option, ``-e <argument>``, that lets you
>>>> set the exception class to handle before executing a script.
>>>>
>>>> With the attached change set, you can now write:
>>>>
>>>> squeak Cuis.image -e nil -s myScript.st
>>>>
>>>> If ``myScript.st`` contains an error, a Debugger
>>>> will pop up.
>>>>
>>>>
>>>> HTH and Best Regards,
>>>>
>>>> Gerald
>>>>
>>>
>>
>
>



More information about the Cuis-dev mailing list