Skip to content

Commit

Permalink
ttools: allow environment-sensitive parameter reporting
Browse files Browse the repository at this point in the history
New interface DynamicTask introduced for tasks with parameter lists
that are sensitive to the environment.  This allows the help system
to be more helpful at runtime.
  • Loading branch information
mbtaylor committed Oct 3, 2014
1 parent d1e7b30 commit 1096f14
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 6 deletions.
34 changes: 34 additions & 0 deletions ttools/src/main/uk/ac/starlink/ttools/task/DynamicTask.java
@@ -0,0 +1,34 @@
package uk.ac.starlink.ttools.task;

import uk.ac.starlink.task.Environment;
import uk.ac.starlink.task.Parameter;
import uk.ac.starlink.task.TaskException;

/**
* Extends the Task interface by methods which allow task parameters to be
* queried based on a (at least partially) populated Environment.
* By default, Tasks report the Parameters they use so that the on-line
* help system can supply that information to users at runtime.
* However, for some tasks the parameters in use depend on the value of
* other parameters. This interface allows tasks to make that information
* available. The method implementations are considered to be on a
* best-efforts basis, it is not guaranteed that they will be able to
* report all the parameters. This information is only used for user help.
*
* @author Mark Taylor
* @since 19 Sep 2014
*/
public interface DynamicTask {

/**
* Attempts to find a parameter with a given name that might be used
* by this task in the content of the given environment.
* This ought not to result in additional prompts to the user.
*
* @param env execution environment
* @param paramName requested parameter name
* @return parameter with the given name, or null
*/
Parameter getParameterByName( Environment env, String paramName )
throws TaskException;
}
22 changes: 16 additions & 6 deletions ttools/src/main/uk/ac/starlink/ttools/task/LineInvoker.java
Expand Up @@ -252,17 +252,17 @@ else if ( arg.equals( "-stderr" ) && it.hasNext() ) {
task = taskFactory_.createObject( taskName );
String[] taskArgs = (String[])
argList.toArray( new String[ 0 ] );
LineWord[] words = new LineWord[ taskArgs.length ];
for ( int i = 0; i < taskArgs.length; i++ ) {
words[ i ] = new LineWord( taskArgs[ i ] );
}
env.setWords( words );
String helpText = helpMessage( env, task, taskName, taskArgs );
if ( helpText != null ) {
out.println( "\n" + helpText );
return 0;
}
else {
LineWord[] words = new LineWord[ taskArgs.length ];
for ( int i = 0; i < taskArgs.length; i++ ) {
words[ i ] = new LineWord( taskArgs[ i ] );
}
env.setWords( words );
long start = System.currentTimeMillis();
Executable exec = task.createExecutable( env );
String[] unused = env.getUnused();
Expand Down Expand Up @@ -421,7 +421,8 @@ private void logParameterValues( String taskName,
* @return help text, or null
*/
private String helpMessage( LineTableEnvironment env, Task task,
String taskName, String[] taskArgs ) {
String taskName, String[] taskArgs )
throws TaskException {
for ( int i = 0; i < taskArgs.length; i++ ) {
String arg = taskArgs[ i ];
String helpFor = null;
Expand Down Expand Up @@ -460,6 +461,15 @@ else if ( helpFor != null ) {
}
}

/* If that fails, look for environment-sensitive parameters. */
if ( task instanceof DynamicTask ) {
Parameter param =
((DynamicTask) task).getParameterByName( env, helpFor );
if ( param != null ) {
return getParamHelp( env, taskName, param );
}
}

/* If that fails, treat the special case of plotting parameters
* which have per-table or per-subset suffixes.
* This allows you to get help for parameter xdata without
Expand Down

0 comments on commit 1096f14

Please sign in to comment.