1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

An empty-arg tolerant @full[…]

Discussion in 'T&T - Functions' started by X A, Oct 29, 2012.

  1. X A

    X A

    Joined:
    Oct 29, 2012
    Messages:
    11
    Likes Received:
    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]`
     
  2. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,784
    Likes Received:
    29
    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.
     
  3. X A

    X A

    Joined:
    Oct 29, 2012
    Messages:
    11
    Likes Received:
    0
    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)).
     
  4. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,277
    Likes Received:
    38
    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.
     
  5. X A

    X A

    Joined:
    Oct 29, 2012
    Messages:
    11
    Likes Received:
    0
    ^ 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! ;)
     
  6. dcantor

    Joined:
    May 29, 2008
    Messages:
    505
    Likes Received:
    3
    That can be fixed with
    Code:
    %@if[%@len[x%1] ne 1,"%@full[%1]",%2]
    
     
  7. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,784
    Likes Received:
    29
    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)`
     
  8. X A

    X A

    Joined:
    Oct 29, 2012
    Messages:
    11
    Likes Received:
    0
    @ 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.
     
  9. X A

    X A

    Joined:
    Oct 29, 2012
    Messages:
    11
    Likes Received:
    0
    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. ;)
     
  10. Charles Dye

    Charles Dye Super Moderator
    Staff Member

    Joined:
    May 20, 2008
    Messages:
    3,277
    Likes Received:
    38
    @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?)
     
  11. dcantor

    Joined:
    May 29, 2008
    Messages:
    505
    Likes Received:
    3
    So sorry.
     
  12. X A

    X A

    Joined:
    Oct 29, 2012
    Messages:
    11
    Likes Received:
    0
    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.
     
  13. vefatica

    Joined:
    May 20, 2008
    Messages:
    7,784
    Likes Received:
    29
    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.
     
  14. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
     
  15. Steve Fabian

    Joined:
    May 20, 2008
    Messages:
    3,523
    Likes Received:
    4
    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.
     
  16. MaartenG

    Joined:
    Aug 3, 2016
    Messages:
    361
    Likes Received:
    8
    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]"]`
    
     
    #16 MaartenG, Feb 1, 2017
    Last edited: Feb 1, 2017

Share This Page