[Cuis-dev] [IMPROV] Configurable command line exception handling
Gerald Klix
cuis.01 at klix.ch
Sat Jan 14 07:39:21 PST 2023
Hi Juan,
I had a closer look at your implementation.
It is less code and looks simpler, but it uses offsets and indexes,
therefore I find it harder to understand.
It has the following drawbacks/missing features:
1. It is not possible to pass options to scripts, they are are all consumed
by the parsing code and then ether honored or ignored.
2. In order to implement #lastCommandLineArguments properly, you have
to look *ahead* for a --.
Of course before parsing, the arguments after -- can be stored in
an instance variable
I am against implementing feature 1, because
it implies once we add new options understood by Cuis' image,
the options passed to the script would change in a rather surprising way.
The attached changes set implements feature 2.
It only adds an instance variable and about 7 lines of code.
With it you can do something like:
--- snip ---
squeak -vm-display-null -headless Cuis6.0-5632cli2.image -d "Smalltalk
scriptingArguments do: [ :arg | StdIOWriteStream stdout nextPutAll: arg;
space]. StdIOWriteStream stdout newLine" a b c dee -d "Smalltalk
scriptingArguments do: [ :arg | StdIOWriteStream stdout nextPutAll: arg;
space]. StdIOWriteStream stdout newLine" z y x why -q -- 1 2 3 -o
--- snap ---
and get the following output:
--- snip ---
a b c dee 1 2 3 -o
z y x why 1 2 3 -o
--- snap ---
I suppose that should support all sensible uses cases.
I also presume, that all requirements of Dr. Geo are fulfilled.
HTH and Best Regards,
Gerald
On 1/13/23 8:21 PM, Juan Vuletich wrote:
> Hi Gerald,
>
> Please check the attach. Besides being simpler and hopefully easier to
> understand, it allows each script to have its own set of arguments.
>
> Perhaps we could add another getter to SystemDictionary, something
> like #lastCommandLineArguments or such, to fetch what's after '--'.
>
> Thanks,
>
> On 1/13/2023 1:18 PM, Gerald Klix via Cuis-dev wrote:
>> I never received this message, therefore I am reseinding it:
>>
>>
>> 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
>>>>>>
>>>>>
>>>>
>>>
>>>
>>
>>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20230114/1f17cf11/attachment-0001.htm>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: 5634-BetterDoubleDashHandling-GeraldKlix-2023Jan14-13h57m-KLG.001.cs.st
Type: application/vnd.sailingtracker.track
Size: 4244 bytes
Desc: not available
URL: <http://lists.cuis.st/mailman/archives/cuis-dev/attachments/20230114/1f17cf11/attachment-0001.bin>
More information about the Cuis-dev
mailing list