You could also have your script pop up a dialog to type the args into.

On Wednesday, December 11, 2024 at 12:03:30 PM UTC-5 jkn wrote:

> Thanks for the comments/encouragement Edward
>
>     I too have a few scripts such as the above, which I customise on an ad 
> hoc basis. I'm not trying to eliminate that kind of fun ;-). This is for 
> the situation when I have a few fixed variants of operation.
>
> I am making progress my following Thomas' pointer to the use of k.get1arg, 
> to prompt and get a return parameter from the minibuffer. As mentioned this 
> is not my favourite approach, but it will do as a start.
>
> Thanks for the pointer to the use of c.interactive.* . I enjoy wandering 
> down these little passages of Leo's design...
>
> PS: TIL about g.cls(), used in your script above! ;-)
>
>     Regards
>     Jon N
>
>
>
>
> On Wednesday, December 11, 2024 at 4:23:09 PM UTC Edward K. Ream wrote:
>
>> On Monday, December 9, 2024 at 3:36:37 PM UTC-6 jkn wrote:
>>
>> I've been meaning to ask this for ever...Is there a way to pass 
>> argument(s) to leo @command-s?
>>
>>
>> Hi Jon,
>>
>> Good question. The short answer is "no", but there are workarounds.
>>
>> I often use bespoke scripts that I customize by hand. For example, here 
>> is a script that I use to import multiple files:
>>
>> @language python
>> """Recursively import all python files in a directory and clean the 
>> result."""
>> @tabwidth -4 # For a better match.
>> g.cls()
>> dir_ = r'<<path to a folder>>  # <--- customize as needed
>> c.recursiveImport(
>>     dir_=dir_,
>>     kind = '@clean', # '@auto', '@clean', '@nosent','@file',
>>     recursive = True,
>>     safe_at_file = True,
>>     theTypes = ['.py',],
>>     verbose = True,
>> )
>> if 1:
>>     last = c.lastTopLevel()
>>     last.expand()
>>     if last.hasChildren():
>>         last.firstChild().expand()
>>     c.redraw(last)
>> print('Done')
>>
>> I will typically execute this script several times, so it would be *less* 
>> convenient to "generalize" it by asking for a path.
>>
>> *Other workarounds*
>>
>> It's almost always simplest to write your own scripts. But if you 
>> *really* want to write a general command, Leo provides ways to do that.
>>
>> The *big hint*: look for code in Leo's codebase that does something 
>> similar to what you want. For example, Leo's find commands prompt for one 
>> or more arguments. The code isn't pretty. *find.find-next* and 
>> *find.change-all* show how to set up the required state machines.
>>
>> The node *c.interactive** in leoCommands.py contains helpers that set up 
>> state machines.
>>
>> *Summary*
>>
>> Writing bespoke scripts is almost always the simplest solution.
>>
>> Other solutions involve state machines. I would only consider such 
>> approaches in extraordinary circumstances.
>>
>> HTH.
>>
>> Edward
>>
>

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion visit 
https://groups.google.com/d/msgid/leo-editor/71e4855a-8fa3-48a1-8d5f-ff4efebb0bc7n%40googlegroups.com.

Reply via email to