Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!

An empty-arg tolerant @full[…]

Oct
11
0
The @full[] built-in function sadly leads to an error message displayed, if the argument is empty.

This is an eg issue when defining an alias to invoke a plain text editor to alternatively open an existing (or implicitly created) file that should be @full-expanded -or- fire it up without a given file:

alias ue=`"%programfiles%\UltraEdit\Uedit32.exe" @full[%1]`

would produce an error message if invoked without an argument.


This function with kind of a delayed expansion hack is what I came up with to my rescue (.ini escaping):

fullne=%@if["%@unquote[%1]"=="",%2,"%%%@left[1,@]full[%1]"]

You can call it with a second parameter specifying a default value to be used if the first one is empty. The above mentioned alias would then read as simply

alias ue=`"%programfiles%\UltraEdit\Uedit32.exe" @fullne[%1]`
 
I use this alias.
Code:
v:\> alias e
for %f in ( %$ ) if not exist %f touch /q /c %f & textpad %$
It will start my (multi-file capable) editor with several files, creating the ones that don't exist. Perhaps you could adapt it to your satisfaction.
 
Thanks for your feedback.

The purpose of this function, though, is not to deal with non existing files, but with an empty arguments list while still applying @full[] when there are arguments – and all this in one line (so no straight forward @if[] usable (in a way knowledgable to me, at least)).
 
The purpose of this function, though, is not to deal with non existing files, but with an empty arguments list while still applying @full[] when there are arguments – and all this in one line (so no straight forward @if[] usable (in a way knowledgable to me, at least)).

I think I'd try something like %@if[%@len[%1] ne 0,"%@full[%1]",%2]

... but in fact I haven't tried it; it might not work.
 
^ That leads to an error message if %1 is empty, because @if[]'s arguments are both evaluated regardless of the condition evaluation. That's been the issue in the first place and the reason I come up with my work-around! ;)
 
^ That leads to an error message if %1 is empty, because @if[]'s arguments are both evaluated regardless of the condition evaluation. That's been the issue in the first place and the reason I come up with my work-around! ;)
I think I'd try something like %@if[%@len[%1] ne 0,"%@full[%1]",%2]

... but in fact I haven't tried it; it might not work.
^ That leads to an error message if %1 is empty, because @if[]'s arguments are both evaluated regardless of the condition evaluation. That's been the issue in the first place and the reason I come up with my work-around! ;)

That can be fixed with
Code:
%@if[%@len[x%1] ne 1,"%@full[%1]",%2]
 
^ That leads to an error message if %1 is empty, because @if[]'s arguments are both evaluated regardless of the condition evaluation. That's been the issue in the first place and the reason I come up with my work-around! ;)
Right! (It has always worked that way.) So you need a scheme in which @FULL is not evaluated if not necessary. That's what IF and IFF are for.

Code:
alias ed `if %# == 1 (editor "%@full[%1]") else (editor)`
 
@ dcantor: No, I respectfully disagree, it cannot.

The error is thrown by @full[], not @if[] or @len[] – but it's thrown due to @if[]'s habbit to expand %vars/fcns first, then eval and apply the condition.
 
So you need a scheme in which @FULL is not evaluated if not necessary.
Exactly. And that's what my function provides as a function instead of an alias.

___
By the way: I did not pose a question, I shared (what I humbly consider) a solution. ;)
 
That can be fixed with
Code:
%@if[%@len[x%1] ne 1,"%@full[%1]",%2]

@LEN doesn't mind an empty string; it just returns 0. It's the @FULL that will complain if %1 is empty. XA is right; my approach is bogus.

(But to pick a nit, if %1 is undefined, then %2 must also be undefined, right?)
 
@ dcantor: No, I respectfully disagree, it cannot.

The error is thrown by @full[], not @if[] or @len[] – but it's thrown due to @if[]'s habbit to expand %vars/fcns first, then eval and apply the condition.
So sorry.
 
(But to pick a nit, if %1 is undefined, then %2 must also be undefined, right?)
Not in a function (even not necessarily with an alias or batch file, either, as we test "empty-stringiness" rather than un/definedness). In a function you have the comma separating parameters.
 
Right! (It has always worked that way.) So you need a scheme in which @FULL is not evaluated if not necessary. That's what IF and IFF are for.

Code:
alias ed `if %# == 1 (editor "%@full[%1]") else (editor)`
Or, patch up an earlier-suggested strategy by delaying the expansion of @FULL.
Code:
alias edit `editor %@if[%# == 1,"%%@full[%1]"]`
If there is no %1, %IF's second parameter becomes "%full[]" which is not later evaluated. If %1 exists, it becomes "%@full[file]" which is evaluated again when the editor command is executed.
 
Sorry, confusion... The IF DEFINED test does not work for batch file or alias parameters. Though logically if there is no first parameter, there cannot be a second, third, etc. parameter, but the pseudovariables %1, %2, etc. are always present ("defined"), albeit as empty strings if not explicitly passed, thus only by comparison with an empty string (explicitly OR implicitly by checking its length using @LEN[]) can its presence or absence be determined. In some circumstances it is possible to have an empty %1 and non-empty %2, esp. if you first strip quotation marks.

X A:
Since you may wish to use more than one file passed to the external, and each must be passed through @FULL, I would use a batch file instead of an alias, and would make the default file its first parameter. You can easily loop through parameters 2 to %$ building the command line for the external, and put the result into the final command line. You would virtually never notice the slight delay accessing the batch file, and it is so much more flexible (as well as easier to debug) than an alias or a UDF.
 
The easiest way around all this, is to let the %@FULL[...] function be "computed" outside the @IF by giving it an extra %: %%@FULL[...]

(And %@FULL will never get there if there is no %1 given)

Code:
alias TEST=`notepad %@IF["%1" == "",,%%@FULL[%1]]`


EDIT: apparently %@FULL doesn't use quotes with the resulting filenames.
Attempt nr. 2:
Code:
alias TEST=`notepad %@IF["%1" == "",,"%%@FULL[%1]"]`
 
Last edited:
Back
Top