Is nested onfail unavoidable here? [SOLVED]

Is it necessary to do multiple path of onfail to do this? file_name has no extension. And the last onfail is intended to suggest existing local files with empty or .pal extension.

	l[-1]
	{
		rm.
		ib ${file_name}
		onfail
			l[] {
				ib ${file_name}.pal
				onfail
					closest_args=${rep_sug_args\ $c_arg,,1,75%,0%,1,0,100%,10%,1,",./",${rep_local_files\ ,pal}}
					if !same('$closest_args','--0000e0')
						error "File '"$c_arg"' does not exist! Expected one of the files (case-insensitive): "$closest_args
					else
						error no_closest_match_found
					fi
			}
	}

So, basically, it’s this:

 try importing file without extension
 onfail
  try importing file with extension
  onfail
    suggest files to import

Could the language be modified to allow for multiple onfail?

Also, I ended up on using 3 nested onfail.

It looks like this for rep_import_pal

try importing file without extension
	onfail
		try importing file with extension .pal
		onfail
			try importing file with extension .pal
			onfail
				suggest files to import

And the real code snip:

	l[-1]
	{
		rm.
		ib ${file_name}
		onfail
			l[] {
				ib ${file_name}.pal
				onfail
					l[] {
						ib ${file_name}.PAL
						onfail
							closest_args=${rep_sug_args\ $c_arg,,1,75%,0%,1,0,100%,10%,1,\",./\",{:${rep_local_files\ ,pal,PAL}}}
							if !same('$closest_args','--0000e0')
								error[0--1] "File '"$c_arg"' does not exist! Expected one of the files (case-insensitive): "$closest_args
							else
								error[0--1] no_closest_match_found
							fi
					}
			}
	}

Actually, found the solution that fix the code smell:

	a0,a1,a2=,.pal,.PAL

	success=0
	repeat 3 {
		l[] {
			ib ${file_name}${a$>}
			success=1
			onfail
				skip ,
		}
		if $success break fi
	}

	if !$success
		closest_args=${rep_sug_args\ $c_arg,,1,75%,0%,1,0,100%,10%,1,\",./\",{:${rep_local_files\ ,pal,PAL}}}
		if !same('$closest_args','--0000e0')
			error[0--1] "File '"$c_arg"' does not exist! Expected one of the files (case-insensitive): "$closest_args
		else
			error[0--1] no_closest_match_found
		fi
	fi

How did you test this…

Assigning success=1 inside a command definition wouldn’t change the success=0 outside the command definition. They have different scope.

A variable definition is always local to the current command except when it starts with an underscore _. In that case, it becomes also accessible by any command invoked outside the current command scope (global variable).

Page 5 of the Handbook.

I just ran it and it behaved as I expected to. Because the variable is still within command scope. When ib fails it immediately goes to onfail block. Otherwise success variable is assigned 1.

the variable is local to a command, not local to a code block, so it can be accessed indeed inside and outside local, repeat, for,... blocks.

1 Like