Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for flag parameters in comments of a flag #18

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/tink/cli/DocFormatter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,5 @@ typedef DocCommand = {
typedef DocFlag = {
aliases:Array<String>,
names:Array<String>,
doc:String,
doc:String
}
6 changes: 0 additions & 6 deletions src/tink/cli/Macro.hx
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ class Macro {
static var infoCache = new TypeMap<ClassInfo>();
static var routerCache = new TypeMap<ComplexType>();
static var docCache = new TypeMap<Expr>();
static var TYPE_STRING = Context.getType('String');
static var TYPE_STRINGLY = Context.getType('tink.Stringly');

public static function build() {
switch Context.getLocalType() {
Expand Down Expand Up @@ -371,10 +369,6 @@ class Macro {

// flag is marked as "required" (will be prompted when missing) if there is no default expr
var isRequired = field.expr() == null && !field.meta.has(':optional');
if(isRequired && !TYPE_STRINGLY.unifiesWith(field.type) && !TYPE_STRING.unifiesWith(field.type)) {
var type = field.type.toComplex().toString();
field.pos.error('$type is not supported. Please use a custom abstract to handle it. See https://github.com/haxetink/tink_cli#data-types');
}

addFlag(flags, aliases, isRequired);

Expand Down
29 changes: 25 additions & 4 deletions src/tink/cli/doc/DefaultFormatter.hx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,20 @@ class DefaultFormatter implements DocFormatter<String> {
}

if(spec.flags.length > 0) {
function getParamDesc(flag:DocFlag) {
return if(flag.doc != null) {
var ido = flag.doc.indexOf('@param');
if(ido > -1) {
var iap = ido + 6;
StringTools.trim(flag.doc.substr(iap, flag.doc.indexOf('\n', iap) - iap));
} else {
"";
}
} else {
"";
}
}

function nameOf(flag:DocFlag) {
var variants = flag.names.join(', ');
if(flag.aliases.length > 0) variants += ', ' + flag.aliases.map(function(a) return '-$a').join(', ');
Expand All @@ -71,20 +85,27 @@ class DefaultFormatter implements DocFormatter<String> {

var maxFlagLength = spec.flags.fold(function(flag, max) {
var name = nameOf(flag);
name += ' ${getParamDesc(flag)}';
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems the quotes are unnecessary?

if(name.length > max) max = name.length;
return max;
}, 0);

function addFlag(name:String, doc:String) {
if(doc == null) doc = '';
addLine(indent(name.lpad(' ', maxFlagLength) + ' : ' + indent(doc, maxFlagLength + 3).trim(), 6));
var ep = ~/^@param.*$/gim;
function addFlag(name:String, doc:String, paramDescription:String) {
if(doc == null) {
doc = '';
} else {
//Filter out the lines with @param
doc = ep.map(doc, function(e : EReg) return "");
}
addLine(indent(('$name $paramDescription').rpad(' ', maxFlagLength) + ' : ' + indent(doc, maxFlagLength + 3).trim(), 6));
}

addLine('');
addLine(' Flags:');

for(flag in spec.flags) {
addFlag(nameOf(flag), formatDoc(flag.doc));
addFlag(nameOf(flag), formatDoc(flag.doc), getParamDesc(flag));
}
}

Expand Down
1 change: 1 addition & 0 deletions tests/RunTests.hx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class RunTests {
new TestAliasDisabled(),
new TestPrompt(),
new TestOptional(),
new TestDoc(),
])).handle(Runner.exit);

}
Expand Down
56 changes: 56 additions & 0 deletions tests/TestDoc.hx
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package;

import tink.cli.*;
import tink.Cli;
import tink.unit.Assert.*;
import haxe.ds.StringMap;

using tink.CoreApi;

@:asserts
class TestDoc {
public function new() {}

public function doc() {
var command = new FlagDoc();

var result =
'\n' +
' Usage: root\n\n' +
' Flags:\n' +
' --name, -n : No parameter\n' +
' --path, -p <directory> : Do search in path\n' +
' --unusedParameter, -u <parameter> : Do search in path\n';

result = StringTools.replace(result, " ", "");
asserts.assert(result == StringTools.replace(Cli.getDoc(command, new tink.cli.doc.DefaultFormatter("root")), " ", ""));
return asserts.done();
}
}

class FlagDoc extends DebugCommand {

/**
No parameter
**/
@:flag('name')
public var name:String = null;

/**
@param <directory>
Do search in path
**/
@:flag('path')
public var path:String = null;

/**
@param <parameter>
Do search in path
@param unused parameter
**/
@:flag('unusedParameter')
public var unusedParameter:String = null;

@:defaultCommand
public function run(args:Rest<String>) {}
}