Skip to content

Commit

Permalink
move the help message code to an observer
Browse files Browse the repository at this point in the history
  • Loading branch information
nvnieuwk committed Dec 19, 2024
1 parent 669653b commit 69095b3
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import org.yaml.snakeyaml.Yaml

@Slf4j
@CompileStatic
class SchemaValidator extends PluginExtensionPoint {
class ValidationExtension extends PluginExtensionPoint {

final List<String> NF_OPTIONS = [
// Options for base `nextflow` command
Expand Down Expand Up @@ -152,24 +152,6 @@ class SchemaValidator extends PluginExtensionPoint {
// Help message logic
def Map params = (Map)session.params ?: [:]
config = new ValidationConfig(session?.config?.navigate('validation') as Map, params)
def Boolean containsFullParameter = params.containsKey(config.help.fullParameter) && params[config.help.fullParameter]
def Boolean containsShortParameter = params.containsKey(config.help.shortParameter) && params[config.help.shortParameter]
if (config.help.enabled && (containsFullParameter || containsShortParameter)) {
def String help = ""
def HelpMessage helpMessage = new HelpMessage(config, session)
help += helpMessage.getBeforeText()
if (containsFullParameter) {
log.debug("Printing out the full help message")
help += helpMessage.getFullHelpMessage()
} else if (containsShortParameter) {
log.debug("Printing out the short help message")
def paramValue = params.get(config.help.shortParameter)
help += helpMessage.getShortHelpMessage(paramValue instanceof String ? paramValue : "")
}
help += helpMessage.getAfterText()
log.info(help)
System.exit(0)
}

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package nextflow.validation

import groovy.util.logging.Slf4j

import nextflow.processor.TaskHandler
import nextflow.trace.TraceObserver
import nextflow.trace.TraceRecord
import nextflow.Session

@Slf4j
class ValidationObserver implements TraceObserver {

@Override
void onFlowCreate(Session session) {
// Help message logic
def Map params = (Map)session.params ?: [:]
def ValidationConfig config = new ValidationConfig(session?.config?.navigate('validation') as Map, params)
def Boolean containsFullParameter = params.containsKey(config.help.fullParameter) && params[config.help.fullParameter]
def Boolean containsShortParameter = params.containsKey(config.help.shortParameter) && params[config.help.shortParameter]
if (config.help.enabled && (containsFullParameter || containsShortParameter)) {
def String help = ""
def HelpMessage helpMessage = new HelpMessage(config, session)
help += helpMessage.getBeforeText()
if (containsFullParameter) {
log.debug("Printing out the full help message")
help += helpMessage.getFullHelpMessage()
} else if (containsShortParameter) {
log.debug("Printing out the short help message")
def paramValue = params.get(config.help.shortParameter)
help += helpMessage.getShortHelpMessage(paramValue instanceof String ? paramValue : "")
}
help += helpMessage.getAfterText()
log.info(help)
System.exit(0)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package nextflow.validation

import nextflow.Session
import nextflow.trace.TraceObserver
import nextflow.trace.TraceObserverFactory

class ValidationObserverFactory implements TraceObserverFactory {

@Override
Collection<TraceObserver> create(Session session) {
// Only enable the trace observer when a help message needs to be printed
final enabled = session.config.navigate('validation.help.enabled')
return enabled ? [ new ValidationObserver() ] : []
}
}
3 changes: 2 additions & 1 deletion plugins/nf-schema/src/resources/META-INF/extensions.idx
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
nextflow.validation.SchemaValidator
nextflow.validation.ValidationExtension
nextflow.validation.ValidationObserverFactory

0 comments on commit 69095b3

Please sign in to comment.