Skip to content

Commit

Permalink
Merge pull request #2 from nextflow-io/picocli
Browse files Browse the repository at this point in the history
Picocli
  • Loading branch information
edgano authored Jan 25, 2018
2 parents f67c670 + 729144c commit 3351de4
Show file tree
Hide file tree
Showing 58 changed files with 5,033 additions and 1,008 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ dependencies {
compile("ch.grengine:grengine:1.2.1") { exclude group: 'org.codehaus.groovy', module: 'groovy-all' }
compile "commons-lang:commons-lang:2.6"
compile "com.beust:jcommander:1.35"
compile('info.picocli:picocli:2.2.1')
compile("com.esotericsoftware.kryo:kryo:2.24.0") { exclude group: 'com.esotericsoftware.minlog', module: 'minlog' }
compile('org.iq80.leveldb:leveldb:0.7')
compile('org.eclipse.jgit:org.eclipse.jgit:4.8.0.201706111038-r')
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/nextflow/Session.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -364,9 +364,12 @@ class Session implements ISession {
Boolean isEnabled = config.navigate('report.enabled') as Boolean
if( isEnabled ) {
String fileName = config.navigate('report.file')
def maxTasks = config.navigate('report.maxTasks', ReportObserver.DEF_MAX_TASKS) as int
if( !fileName ) fileName = ReportObserver.DEF_FILE_NAME
def report = (fileName as Path).complete()
def observer = new ReportObserver(report)
observer.maxTasks = maxTasks

result << observer
}
}
Expand Down
16 changes: 15 additions & 1 deletion src/main/groovy/nextflow/cli/CliOptions.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -19,65 +19,79 @@
*/

package nextflow.cli

import com.beust.jcommander.DynamicParameter
import com.beust.jcommander.Parameter
import picocli.CommandLine

/**
* Main application command line options
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@CommandLine.Command
class CliOptions {

/**
* The packages to debug
*/
@Parameter(hidden = true, names='-debug')
@CommandLine.Option(hidden = true, names=['--debug'])
List<String> debug

@Parameter(names=['-log'], description = 'Set nextflow log file path')
@CommandLine.Option(names=['--log'])
String logFile

@Parameter(names=['-c','-config'], description = 'Add the specified file to configuration set')
@CommandLine.Option(names=['-c','--config'])
List<String> userConfig

@Parameter(names=['-C'], description = 'Use the specified configuration file(s) overriding any defaults')
@CommandLine.Option(names=['-C'])
List<String> config

/**
* the packages to trace
*/
@Parameter(names='-trace', hidden = true)
@CommandLine.Option(names=['--trace'])
List<String> trace

/**
* Enable syslog appender
*/
@Parameter(names = ['-syslog'], description = 'Send logs to syslog server (eg. localhost:514)' )
@CommandLine.Option(names=['--syslog'])
String syslog

/**
* Print out the version number and exit
*/
@Parameter(names = ['-v','-version'], description = 'Print the program version')
@CommandLine.Option(names=['-v','--version'])
boolean version

/**
* Print out the 'help' and exit
*/
@Parameter(names = ['-h'], description = 'Print this help', help = true)
@CommandLine.Option(names=['-h','--help'])
boolean help

@Parameter(names = ['-q','-quiet'], description = 'Do not print information messages' )
@CommandLine.Option(names=['-q','--quite'])
boolean quiet

@Parameter(names = ['-bg'], description = 'Execute nextflow in background', arity = 0)
@CommandLine.Option(names=['--bg'], arity='0')
boolean background

@DynamicParameter(names = ['-D'], description = 'Set JVM properties' )
@CommandLine.Option(names=['-D'])
Map<String,String> jvmOpts = [:]

@Parameter(names = ['-self-update'], description = 'Update nextflow to the latest version', arity = 0, hidden = true)
@CommandLine.Option(names=['-u','--self-update'], arity = '0', hidden = true)
boolean selfUpdate

}
2 changes: 2 additions & 0 deletions src/main/groovy/nextflow/cli/CmdClean.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import nextflow.exception.AbortOperationException
import nextflow.file.FileHelper
import nextflow.trace.TraceRecord
import nextflow.util.HistoryFile.Record
import picocli.CommandLine

/**
* Implements cache clean up command
Expand All @@ -44,6 +45,7 @@ import nextflow.util.HistoryFile.Record
@Slf4j
@CompileStatic
@Parameters(commandDescription = "Clean up project cache and work directories")
@CommandLine.Command
class CmdClean extends CmdBase implements CacheBase {

static final public NAME = 'clean'
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/nextflow/cli/CmdClone.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import nextflow.exception.AbortOperationException
import nextflow.scm.AssetManager
import picocli.CommandLine

/**
* CLI sub-command clone
*
Expand All @@ -33,6 +35,7 @@ import nextflow.scm.AssetManager
@Slf4j
@CompileStatic
@Parameters(commandDescription = "Clone a project into a folder")
@CommandLine.Command
class CmdClone extends CmdBase implements HubOptions {

static final public NAME = 'clone'
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/nextflow/cli/CmdCloud.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import nextflow.exception.AbortOperationException
import nextflow.ui.TableBuilder
import nextflow.ui.TextLabel
import nextflow.util.SysHelper
import picocli.CommandLine

/**
* Implements the `cloud` command
*
Expand All @@ -51,6 +53,7 @@ import nextflow.util.SysHelper
@Slf4j
//@CompileStatic -- Don't use. It causes a weird exception: CmdCloud$LaunchMaster cannot be cast to nextflow.cli.CmdCloud
@Parameters(commandDescription = "Manage Nextflow clusters in the cloud")
@CommandLine.Command
class CmdCloud extends CmdBase implements UsageAware {

interface SubCmd {
Expand Down
109 changes: 7 additions & 102 deletions src/main/groovy/nextflow/cli/CmdConfig.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,8 @@ import groovy.util.logging.Slf4j
import nextflow.config.ConfigBuilder
import nextflow.exception.AbortOperationException
import nextflow.scm.AssetManager
import org.codehaus.groovy.runtime.InvokerHelper
import nextflow.util.ConfigHelper
import picocli.CommandLine

/**
* Prints the pipeline configuration
Expand All @@ -39,6 +40,7 @@ import org.codehaus.groovy.runtime.InvokerHelper
@Slf4j
@CompileStatic
@Parameters(commandDescription = "Print a project configuration")
@CommandLine.Command
class CmdConfig extends CmdBase {

static final public NAME = 'config'
Expand Down Expand Up @@ -81,8 +83,7 @@ class CmdConfig extends CmdBase {
.setOptions(launcher.options)
.setBaseDir(base.complete())
.setCmdConfig(this)
.build()
.toConfigObject()
.configObject()

if( printProperties ) {
printProperties(config, stdout)
Expand All @@ -103,57 +104,7 @@ class CmdConfig extends CmdBase {
* @param output The stream where output the formatted configuration notation
*/
protected void printCanonical(ConfigObject config, OutputStream output) {
def writer = new PrintWriter(output)
canonicalFormat(writer,config,0)
writer.flush()
}

private static final String TAB = ' '

private void canonicalFormat(Writer writer, ConfigObject object, int level) {

final keys = object.keySet().sort()

// remove all empty config objects
final itr = keys.iterator()
while( itr.hasNext() ) {
final key = itr.next()
final value = object.get(key)
if( value instanceof ConfigObject && value.size()==0 ) {
itr.remove()
}
}

for( int i=0; i<keys.size(); i++) {
final key = keys[i]
final value = object.get(key)
if( value instanceof ConfigObject ) {
// add an extra new-line to separate simple values from a config object
if( level==0 && i>0 ) {
writer.write('\n')
}

writer.write(TAB*level)
writer.write(key.toString())
writer.write(' {\n')
canonicalFormat(writer, value, level+1)
writer.write(TAB*level)
writer.write('}\n')

}
else {
// add a new-line to separate simple values from a previous config object
if( level==0 && i>0 && object.get(keys[i-1]) instanceof ConfigObject) {
writer.write('\n')
}

writer.write(TAB*level)
writer.write(key.toString())
writer.write(' = ')
writer.write( InvokerHelper.inspect(value) )
writer.write('\n')
}
}
output << ConfigHelper.toCanonicalString(config)
}

/**
Expand All @@ -163,9 +114,7 @@ class CmdConfig extends CmdBase {
* @param output The stream where output the formatted configuration notation
*/
protected void printProperties(ConfigObject config, OutputStream output) {
def writer = new PrintWriter(output)
writer.write( propertiesFormat(new OrderedProperties(config.toProperties())) )
writer.flush()
output << ConfigHelper.toPropertiesString(config)
}

/**
Expand All @@ -176,9 +125,7 @@ class CmdConfig extends CmdBase {
* @param output The stream where output the formatted configuration notation
*/
protected void printFlatten(ConfigObject config, OutputStream output) {
def writer = new PrintWriter(output, true)
writer.write( flattenFormat(config) )
writer.flush()
output << ConfigHelper.toFlattenString(config)
}

/**
Expand All @@ -192,30 +139,6 @@ class CmdConfig extends CmdBase {
config.writeTo( writer )
}

private String flattenFormat(ConfigObject config) {
final props = new Properties()
config.flatten(props)
final ordered = new OrderedProperties(props)
final result = new StringBuilder()
for( String name : ordered.keys() ) {
result << name << ' = ' << InvokerHelper.inspect(ordered.get(name)) << '\n'
}
result.toString()
}

private String propertiesFormat(Properties properties) {
def buffer = new ByteArrayOutputStream()
properties.store(buffer,null)
buffer.flush()

def result = new StringBuilder()
for( String line : buffer.toString().readLines() ) {
if(line.startsWith('#')) continue
result << line << '\n'
}
result.toString()
}


Path getBaseDir(String path) {

Expand All @@ -232,24 +155,6 @@ class CmdConfig extends CmdBase {

}

/**
* Extends the basic {@link Properties} to provide the ordered enumeration of keys
*/
static class OrderedProperties extends Properties {

OrderedProperties() {}

OrderedProperties( Properties properties ) {
properties.each { key, value ->
this.put(key,value)
}
}

@Override
Enumeration<Object> keys() {
return new Vector<>(super.keySet().sort()).elements()
}

}

}
2 changes: 2 additions & 0 deletions src/main/groovy/nextflow/cli/CmdDrop.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import groovy.transform.CompileStatic
import groovy.util.logging.Slf4j
import nextflow.exception.AbortOperationException
import nextflow.scm.AssetManager
import picocli.CommandLine

/**
* CLI sub-command DROP
Expand All @@ -35,6 +36,7 @@ import nextflow.scm.AssetManager
@Slf4j
@CompileStatic
@Parameters(commandDescription = "Delete the local copy of a project")
@CommandLine.Command
class CmdDrop extends CmdBase {

static final public NAME = 'drop'
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/nextflow/cli/CmdFs.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,14 @@ import nextflow.exception.AbortOperationException
import nextflow.extension.FilesEx
import nextflow.file.FileHelper
import nextflow.file.FilePatternSplitter
import picocli.CommandLine

/**
* Implements `fs` command
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@CommandLine.Command
class CmdFs extends CmdBase implements UsageAware {

static final public NAME = 'fs'
Expand Down
3 changes: 3 additions & 0 deletions src/main/groovy/nextflow/cli/CmdHelp.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,16 @@ package nextflow.cli
import com.beust.jcommander.Parameter
import com.beust.jcommander.Parameters
import groovy.transform.CompileStatic
import picocli.CommandLine

/**
* CLI sub-command HELP
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@CompileStatic
@Parameters(commandDescription = "Print the usage help for a command")
@CommandLine.Command
class CmdHelp extends CmdBase {

static final public NAME = 'help'
Expand Down
7 changes: 6 additions & 1 deletion src/main/groovy/nextflow/cli/CmdInfo.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -30,25 +30,30 @@ import nextflow.Const
import nextflow.exception.AbortOperationException
import nextflow.scm.AssetManager
import nextflow.util.MemoryUnit

import picocli.CommandLine
import picocli.CommandLine.Option
/**
* CLI sub-command INFO
*
* @author Paolo Di Tommaso <paolo.ditommaso@gmail.com>
*/
@CompileStatic
@Parameters(commandDescription = "Print project and system runtime information")
@CommandLine.Command
class CmdInfo extends CmdBase {

static final public NAME = 'info'

@Parameter(description = 'project name')
@CommandLine.Parameters(description = 'Project name', arity = '0..1')
List<String> args

@Parameter(names='-d',description = 'Show detailed information', arity = 0)
@Option(names=['-d'], description = 'Show detailed information', arity = '0..1')
boolean detailed

@Parameter(names='-dd', hidden = true, arity = 0)
@Option(names=['-dd'], description = 'Show more detailed information', arity = '0..1')
boolean moreDetailed

@Override
Expand Down
Loading

0 comments on commit 3351de4

Please sign in to comment.