From f0c739f66bf189e54ff1375549254e87f6adbc83 Mon Sep 17 00:00:00 2001 From: Heongseok Heo Date: Wed, 20 Nov 2013 18:28:26 +0900 Subject: [PATCH] CLI: Working on printing output message dynamically. Replace CmdLineParser and SubCommandHandler of arg4j as custom class. Add HelpCLI dummy class. Add some classes for supporting print usage info. Change-Id: I80486e18a89d5869f5b511ac5dd36e63a4c56389 Signed-off-by: Heongseok Heo --- .../src/org/tizen/ncli/core/CommandInfo.java | 85 ++++++++ .../src/org/tizen/ncli/core/CommandLineParser.java | 133 +++++++++++++ .../src/org/tizen/ncli/core/SubCommandData.java | 57 ++++++ .../src/org/tizen/ncli/core/TizenSubCommand.java | 54 ++++++ .../tizen/ncli/core/TizenSubCommandHandler.java | 129 ++++++++++++ .../src/org/tizen/ncli/core/collection/Tree.java | 74 +++++++ .../src/org/tizen/ncli/ide/shell/AbstractCLI.java | 102 +++++----- .../src/org/tizen/ncli/ide/shell/BuildWebCLI.java | 2 + .../src/org/tizen/ncli/ide/shell/ConfigCLI.java | 2 + .../src/org/tizen/ncli/ide/shell/HelpCLI.java | 42 ++++ .../src/org/tizen/ncli/ide/shell/Main.java | 216 ++++++++++----------- 11 files changed, 730 insertions(+), 166 deletions(-) create mode 100644 org.tizen.ncli.ide/src/org/tizen/ncli/core/CommandInfo.java create mode 100644 org.tizen.ncli.ide/src/org/tizen/ncli/core/CommandLineParser.java create mode 100644 org.tizen.ncli.ide/src/org/tizen/ncli/core/SubCommandData.java create mode 100644 org.tizen.ncli.ide/src/org/tizen/ncli/core/TizenSubCommand.java create mode 100644 org.tizen.ncli.ide/src/org/tizen/ncli/core/TizenSubCommandHandler.java create mode 100644 org.tizen.ncli.ide/src/org/tizen/ncli/core/collection/Tree.java create mode 100644 org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/HelpCLI.java diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/core/CommandInfo.java b/org.tizen.ncli.ide/src/org/tizen/ncli/core/CommandInfo.java new file mode 100644 index 0000000..02c2f4f --- /dev/null +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/core/CommandInfo.java @@ -0,0 +1,85 @@ +/* + * IDE + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Hyeongseok Heo + * Kangho Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + */ +package org.tizen.ncli.core; + + +/** + * This class represent sub command information of Tizen New CLI. + * @author Harry Hyeongseok Heo{@literal } (S-core) + * + * + */ +public class CommandInfo implements Comparable{ + private final String parentCmd; + private final String commandName; + private String usage; + private boolean common; + + public CommandInfo(final String parentCmd, final String name, final String usage, boolean common) { + this.parentCmd = parentCmd; + this.commandName = name; + this.usage = usage; + this.common = common; + } + + /** + * @param rootCmdName + */ + public CommandInfo(final String parentCmd, final String cmdName) { + this.parentCmd = parentCmd; + this.commandName = cmdName; + } + + public String getUsage() { + return usage; + } + + public void setUsage(String usage) { + this.usage = usage; + } + + public boolean isCommon() { + return common; + } + + public void setCommon(boolean common) { + this.common = common; + } + + /* (non-Javadoc) + * @see java.lang.Comparable#compareTo(java.lang.Object) + */ + @Override + public int compareTo(CommandInfo o) { + if( null != o.parentCmd && o.parentCmd.equals(this.parentCmd) ) { + if ( o.commandName != null && o.commandName.equals(this.commandName)) { + return 0; + } + } + return 1; + } + + +} diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/core/CommandLineParser.java b/org.tizen.ncli.ide/src/org/tizen/ncli/core/CommandLineParser.java new file mode 100644 index 0000000..bbd8d1f --- /dev/null +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/core/CommandLineParser.java @@ -0,0 +1,133 @@ +/* + * IDE + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Hyeongseok Heo + * Kangho Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + */ +package org.tizen.ncli.core; + +import java.io.PrintWriter; +import java.io.Writer; +import java.lang.annotation.Annotation; +import java.lang.reflect.AnnotatedElement; +import java.util.List; +import java.util.ResourceBundle; + +import org.kohsuke.args4j.CmdLineException; +import org.kohsuke.args4j.CmdLineParser; +import org.kohsuke.args4j.OptionHandlerFilter; +import org.kohsuke.args4j.spi.FieldSetter; +import org.kohsuke.args4j.spi.OptionHandler; +import org.kohsuke.args4j.spi.Setter; +import org.kohsuke.args4j.spi.SubCommand; +import org.kohsuke.args4j.spi.SubCommands; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Harry Hyeongseok Heo{@literal } (S-core) + * + * + */ +public class CommandLineParser extends CmdLineParser { + private Logger log = LoggerFactory.getLogger(getClass()); + + public CommandLineParser(Object bean) { + super(bean); + } + + + + /* (non-Javadoc) + * @see org.kohsuke.args4j.CmdLineParser#printUsage(java.io.Writer, java.util.ResourceBundle, org.kohsuke.args4j.OptionHandlerFilter) + */ + @Override + public void printUsage(Writer out, ResourceBundle rb, OptionHandlerFilter filter) { + super.printUsage(out, rb, filter); + + } + + + + + + /* (non-Javadoc) + * @see org.kohsuke.args4j.CmdLineParser#parseArgument(java.lang.String[]) + */ + @Override + public void parseArgument(String... args) throws CmdLineException { + try { + super.parseArgument(args); + }catch(CmdLineException e) { + log.trace("CmdLineException occurred.\n{}",e.getMessage()); + throw e; + }finally { + //process parsing subcommand infos + log.trace("make sub command reference infos."); + makeSubcommandData(); + } + } + + + + /* (non-Javadoc) + * @see org.kohsuke.args4j.CmdLineParser#printOption(java.io.PrintWriter, org.kohsuke.args4j.spi.OptionHandler, int, java.util.ResourceBundle, org.kohsuke.args4j.OptionHandlerFilter) + */ + @Override + protected void printOption(PrintWriter out, OptionHandler handler, int len, ResourceBundle rb, + OptionHandlerFilter filter) { + super.printOption(out, handler, len, rb, filter); + log.trace("PrintOption:handler.setter {}",handler.setter); + } + + + + public void makeSubcommandData() { + List arguments2 = getArguments(); + if ( null != arguments2) { + OptionHandler optionHandler = arguments2.get(0); + if( optionHandler instanceof TizenSubCommandHandler) { + log.trace("{}",optionHandler); + TizenSubCommandHandler tizenSubCmdHandler = (TizenSubCommandHandler) optionHandler; + SubCommands subCommands = tizenSubCmdHandler.getSubcommand(); + SubCommand[] subCommandArray = subCommands.value(); + for (int i = 0; i < subCommandArray.length; i++) { + Object instantiate = tizenSubCmdHandler.instantiate(subCommandArray[i]); + TizenSubCommand annotation = instantiate.getClass().getAnnotation(TizenSubCommand.class); + log.trace("SubCommand:{}",subCommandArray[i].name()); + if( null != annotation) { + String subCmdName = ( null == annotation.name())?subCommandArray[i].name():annotation.name(); +// SubCommandData.put(subCmdName,new CommandInfo(subCmdName,annotation.usage(),annotation.common())); + log.trace("SubCommandData:"+subCmdName+"\t{}\t{}", annotation.usage(), annotation.common()); + }else { + log.trace("SubCommandData w/o annotation:{}",subCommandArray[i].name()); +// SubCommandData.put(subCommandArray[i].name(), null); + } + + } + + } + } + } + + + +} diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/core/SubCommandData.java b/org.tizen.ncli.ide/src/org/tizen/ncli/core/SubCommandData.java new file mode 100644 index 0000000..eb4fcd7 --- /dev/null +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/core/SubCommandData.java @@ -0,0 +1,57 @@ +/* + * IDE + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Hyeongseok Heo + * Kangho Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + */ +package org.tizen.ncli.core; + +import java.util.Map; +import java.util.TreeMap; + +import org.kohsuke.args4j.spi.SubCommand; + +/** + * To reference {@link SubCommand} information , load and set sub command information + * on this class. + * @author Harry Hyeongseok Heo{@literal } (S-core) + * + * + */ +public class SubCommandData { + private static final SubCommandData CMD_DATA = new SubCommandData(); + + + private static final Map CMD_TREEMAP = new TreeMap(); + + private SubCommandData() { + } + + protected static void put(CommandInfo parent , CommandInfo child) { + CMD_TREEMAP.put(parent, child); + } + + public static CommandInfo[] getChildren(CommandInfo parent) { + return null; + } + + +} diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/core/TizenSubCommand.java b/org.tizen.ncli.ide/src/org/tizen/ncli/core/TizenSubCommand.java new file mode 100644 index 0000000..418513e --- /dev/null +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/core/TizenSubCommand.java @@ -0,0 +1,54 @@ +/* + * IDE + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Hyeongseok Heo + * Kangho Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + */ +package org.tizen.ncli.core; + +import java.lang.annotation.ElementType; +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; +import java.lang.annotation.Target; + +import org.kohsuke.args4j.spi.SubCommand; + +/** + * This is reference data class for {@link SubCommand} , mainly {@link #usage()} and {@link #common()}. + * @author Harry Hyeongseok Heo{@literal } (S-core) + */ +@Target(ElementType.TYPE) +@Retention(RetentionPolicy.RUNTIME) +public @interface TizenSubCommand { + /** + * This is just for showing information in CLI. + * @return command's name which will be used in CLI. + */ + public String name() default ""; + /** + * @return single line usage text for this sub command + */ + public String usage() default ""; + /** + * @return default is false. set true if this command should show in the usage + */ + public boolean common() default false; +} diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/core/TizenSubCommandHandler.java b/org.tizen.ncli.ide/src/org/tizen/ncli/core/TizenSubCommandHandler.java new file mode 100644 index 0000000..c3da835 --- /dev/null +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/core/TizenSubCommandHandler.java @@ -0,0 +1,129 @@ +/* + * IDE + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Hyeongseok Heo + * Kangho Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + */ +package org.tizen.ncli.core; + +import java.util.AbstractList; +import java.util.HashMap; +import java.util.Map; + +import org.kohsuke.args4j.CmdLineException; +import org.kohsuke.args4j.CmdLineParser; +import org.kohsuke.args4j.OptionDef; +import org.kohsuke.args4j.spi.Messages; +import org.kohsuke.args4j.spi.OptionHandler; +import org.kohsuke.args4j.spi.Parameters; +import org.kohsuke.args4j.spi.Setter; +import org.kohsuke.args4j.spi.SubCommand; +import org.kohsuke.args4j.spi.SubCommandHandler; +import org.kohsuke.args4j.spi.SubCommands; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author Harry Hyeongseok Heo{@literal } (S-core) + * + * + */ +public class TizenSubCommandHandler extends OptionHandler { + private Logger log = LoggerFactory.getLogger(getClass()); + + private SubCommands commands; + private Map commandInfo = new HashMap(); + + public TizenSubCommandHandler(CmdLineParser parser, OptionDef option, Setter setter) { + super(parser, option, setter); + commands = setter.asAnnotatedElement().getAnnotation(SubCommands.class); + if (commands==null) { + throw new IllegalStateException("SubCommandHandler must be used with @SubCommands annotation"); + } + + } + + @Override + public int parseArguments(Parameters params) throws CmdLineException { + String subCmd = params.getParameter(0); + + for (SubCommand c : commands.value()) { + if (c.name().equals(subCmd)) { + setter.addValue(subCommand(c,params)); + return params.size(); // consume all the remaining tokens + } + } + + return fallback(subCmd); + } + + protected int fallback(String subCmd) throws CmdLineException { + throw new CmdLineException(owner, Messages.ILLEGAL_OPERAND.format(option.toString(),subCmd)); + } + + protected Object subCommand(SubCommand c, final Parameters params) throws CmdLineException { + Object subCmd = instantiate(c); + CmdLineParser p = configureParser(subCmd,c); + p.parseArgument(new AbstractList() { + @Override + public String get(int index) { + try { + return params.getParameter(index+1); + } catch (CmdLineException e) { + // invalid index was accessed. + throw new IndexOutOfBoundsException(); + } + } + + @Override + public int size() { + return params.size()-1; + } + }); + return subCmd; + } + + protected CmdLineParser configureParser(Object subCmd, SubCommand c) { + return new CommandLineParser(subCmd); + } + + protected Object instantiate(SubCommand c) { + try { + return c.impl().newInstance(); + } catch (InstantiationException e) { + throw new IllegalStateException("Failed to instantiate "+c,e); + } catch (IllegalAccessException e) { + throw new IllegalStateException("Failed to instantiate "+c,e); + } + } + + @Override + public String getDefaultMetaVariable() { + return "CMD ARGS..."; + } + + public SubCommands getSubcommand() { + return this.commands; + } + + + +} diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/core/collection/Tree.java b/org.tizen.ncli.ide/src/org/tizen/ncli/core/collection/Tree.java new file mode 100644 index 0000000..774b182 --- /dev/null +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/core/collection/Tree.java @@ -0,0 +1,74 @@ +/* + * IDE + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Hyeongseok Heo + * Kangho Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + */ +package org.tizen.ncli.core.collection; + +import java.util.ArrayList; +import java.util.List; + +import org.tizen.ncli.core.CommandInfo; + +/** + * Tree structure collection.
+ *

Initially this is copied from http://stackoverflow.com/questions/3522454/java-tree-data-structure + * But some features are added. + * This is not for general data container like huge size of data.(e.g. file directory structure) + * @author Harry Hyeongseok Heo{@literal } (S-core) + * @param + * @date 2013. 11. 20. + */ +public class Tree { + private Node root; + + public Tree(T rootData) { + root = new Node(); + root.data = rootData; + root.children = new ArrayList>(); + } + + public static class Node { + private T data; + private Node parent; + private List> children; + } + + /** + * Get {@linkplain Node} which has a given commandInfo as a root. + * @param commandInfo + */ + public Node get(CommandInfo commandInfo) { + // TODO Auto-generated method stub + if( root.data.equals(commandInfo) ) { + return root; + }else { + //traverse if there is Node as same as given commandInfo + for (Node child : root.children) { + if(child.data.equals(commandInfo)) { + return child; + } + } + } + return null; + } +} diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/AbstractCLI.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/AbstractCLI.java index 1f793b1..c270a3b 100644 --- a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/AbstractCLI.java +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/AbstractCLI.java @@ -1,51 +1,51 @@ -/* - * IDE - * - * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. - * - * Contact: - * Hyeongseok Heo - * Kangho Kim - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - * - * Contributors: - * - S-Core Co., Ltd - */ -package org.tizen.ncli.ide.shell; - -import java.io.File; -import java.io.PrintWriter; - -import org.kohsuke.args4j.Option; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * Implemented common function of CLI options - * - * @author Harry Hyeongseok Heo{@literal } (S-core) - */ -public abstract class AbstractCLI { - protected Logger log = LoggerFactory.getLogger(getClass()); - - @Option(name = "--", metaVar = "working directory", usage = "Specify where is the base directory for the command") - public File workingDir; - - @Option(name = "--current-workspace-path", metaVar = "current workspace path", usage = "Specify where is the root path for the command as a default") - public String currentWorkspacePath; - - protected PrintWriter output = new PrintWriter(System.out);; - - public abstract void execute(); -} +/* + * IDE + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Hyeongseok Heo + * Kangho Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + */ +package org.tizen.ncli.ide.shell; + +import java.io.File; +import java.io.PrintWriter; + +import org.kohsuke.args4j.Option; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * Implemented common function of CLI options + * + * @author Harry Hyeongseok Heo{@literal } (S-core) + */ +public abstract class AbstractCLI { + protected Logger log = LoggerFactory.getLogger(getClass()); + + @Option(name = "--", metaVar = "working directory", usage = "Specify where is the base directory for the command") + public File workingDir; + + @Option(hidden=true,name = "--current-workspace-path", metaVar = "current workspace path", usage = "Specify where is the root path for the command as a default") + public String currentWorkspacePath; + + protected PrintWriter output = new PrintWriter(System.out);; + + public abstract void execute(); +} diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/BuildWebCLI.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/BuildWebCLI.java index b11314f..4e5fe19 100644 --- a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/BuildWebCLI.java +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/BuildWebCLI.java @@ -8,8 +8,10 @@ import org.kohsuke.args4j.spi.StringOptionHandler; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.tizen.core.ide.BuildWebParameter; +import org.tizen.ncli.core.TizenSubCommand; import org.tizen.ncli.ide.subcommands.BuildWebCLICommand; +@TizenSubCommand(name="build-web" , common=true , usage = "Build sources under the Tizen web project") public class BuildWebCLI extends AbstractCLI { private Logger log = LoggerFactory.getLogger(getClass()); diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/ConfigCLI.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/ConfigCLI.java index 2f1869a..1d4cadb 100644 --- a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/ConfigCLI.java +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/ConfigCLI.java @@ -36,6 +36,7 @@ import org.kohsuke.args4j.CmdLineParser; import org.kohsuke.args4j.Option; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.tizen.ncli.core.TizenSubCommand; import org.tizen.ncli.ide.Tizen; import org.tizen.ncli.ide.TizenCLIConfig; import org.tizen.ncli.ide.config.Configuration; @@ -46,6 +47,7 @@ import org.tizen.ncli.ide.subcommands.ConfigCLICommand; /** * @author Harry Hyeongseok Heo{@literal } (S-core) */ +@TizenSubCommand(name="config" , usage="Set up configurations on the tizen CLI") public class ConfigCLI extends AbstractCLI{ private Map properties = new HashMap(); diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/HelpCLI.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/HelpCLI.java new file mode 100644 index 0000000..0538242 --- /dev/null +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/HelpCLI.java @@ -0,0 +1,42 @@ +/* + * IDE + * + * Copyright (c) 2000 - 2013 Samsung Electronics Co., Ltd. All rights reserved. + * + * Contact: + * Hyeongseok Heo + * Kangho Kim + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * Contributors: + * - S-Core Co., Ltd + */ +package org.tizen.ncli.ide.shell; + +/** + * @author Harry Hyeongseok Heo{@literal } (S-core) + * + * + */ +public class HelpCLI extends AbstractCLI{ + + @Override + public void execute() { + // TODO Auto-generated method stub + + } + + + +} diff --git a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/Main.java b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/Main.java index 7f6855b..712556a 100644 --- a/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/Main.java +++ b/org.tizen.ncli.ide/src/org/tizen/ncli/ide/shell/Main.java @@ -1,115 +1,101 @@ -/** - * - */ -package org.tizen.ncli.ide.shell; - -import java.io.PrintWriter; -import java.text.MessageFormat; -import java.util.List; - -import org.kohsuke.args4j.Argument; -import org.kohsuke.args4j.CmdLineException; -import org.kohsuke.args4j.CmdLineParser; -import org.kohsuke.args4j.OptionHandlerFilter; -import org.kohsuke.args4j.spi.OptionHandler; -import org.kohsuke.args4j.spi.SubCommand; -import org.kohsuke.args4j.spi.SubCommandHandler; -import org.kohsuke.args4j.spi.SubCommands; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * This class is entry point of Tizen New Command Line Interface. All the command line argument would be parsed and then - * proper Command class is called. - * - * @author Harry Hyeongseok Heo{@literal } (S-core) - * - */ -public class Main { - - private Logger log = LoggerFactory.getLogger(Main.class); - - //FIXME Should resolve subcommand usage message problem and usage and metaVar should be externalized! - /* - * usage="Set up configurations on the tizen CLI\n " - + "Create tizen resource\n " - + "Build sources with Tizen native build tools\n " - + "Build sources under the Tizen web project\n " - + "Make a sinature file with a given certificate information \n " - + "Make a tizen package(tpk|wgt) \n " - + "Transfer the package to the target and install on the target \n " - + "Uninstall app. from the selected target with given package id \n " - + "Run a app. at the target \n " - + "Debug a app. at the target" - * */ - @Argument(index = 0, required = true, handler = SubCommandHandler.class , - metaVar="cli-config\n" - + "create\n" - + "build-native\n" - + "build-web\n" - + "sign\n" - + "package\n" - + "install\n" - + "uninstall\n" - + "run\n" - + "debug", - usage="Set up configurations on the tizen CLI\n " - ) - @SubCommands({ @SubCommand(name = "create", impl = CreateCLI.class), - @SubCommand(name = "build-native", impl = BuildNativeCLI.class), - @SubCommand(name = "build-web", impl = BuildWebCLI.class), - @SubCommand(name = "sign", impl = SignCLI.class), - @SubCommand(name = "cli-config", impl = ConfigCLI.class) }) - private AbstractCLI tizenCLI; - - /** - * @param args - */ - public static void main(String[] args) { - Main tizen = new Main(); - tizen.run(args); - - } - - private void run(String[] args) { - CmdLineParser cmdParser = new CmdLineParser(this); - log.trace("Start running Tizen CLI Main class..."); - PrintWriter errorWriter = new PrintWriter(System.err); - try { - cmdParser.setUsageWidth(120); - // Argument parsing - make metadata from annotation. - cmdParser.parseArgument(args); - } catch (CmdLineException e) { - if( args.length > 0) { - errorWriter.println(); - errorWriter.println("Argument is not valid!"); - errorWriter.println(MessageFormat.format("Error: {0}", e.getMessage())); - errorWriter.println(); - }else { - errorWriter.print("Command is needed like belows!"); - errorWriter.println(); - cmdParser.printUsage(errorWriter, null); - errorWriter.println(); - } - errorWriter.flush(); - System.exit(1); - } - - try { - //Execute actual command classes - if (null != this.tizenCLI) { - this.tizenCLI.execute(); - } - }catch(Exception e) { - e.printStackTrace(); - } - - errorWriter.flush(); - if( null != tizenCLI.output) { - tizenCLI.output.flush(); - } - System.exit(1); - - } - -} +/** + * + */ +package org.tizen.ncli.ide.shell; + +import java.io.PrintWriter; +import java.text.MessageFormat; +import java.util.List; + +import org.kohsuke.args4j.Argument; +import org.kohsuke.args4j.CmdLineException; +import org.kohsuke.args4j.CmdLineParser; +import org.kohsuke.args4j.OptionHandlerFilter; +import org.kohsuke.args4j.spi.OptionHandler; +import org.kohsuke.args4j.spi.SubCommand; +import org.kohsuke.args4j.spi.SubCommandHandler; +import org.kohsuke.args4j.spi.SubCommands; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.tizen.ncli.core.CommandLineParser; +import org.tizen.ncli.core.TizenSubCommand; +import org.tizen.ncli.core.TizenSubCommandHandler; + +/** + * This class is entry point of Tizen New Command Line Interface. All the command line argument would be parsed and then + * proper Command class is called. + * + * @author Harry Hyeongseok Heo{@literal } (S-core) + * + */ +public class Main { + + private Logger log = LoggerFactory.getLogger(Main.class); + + //FIXME Should resolve subcommand usage message problem and usage and metaVar should be externalized! + @Argument(index = 0, required = true, handler = TizenSubCommandHandler.class , + metaVar="", + usage="tizen " + ) + @SubCommands({ @SubCommand(name = "create", impl = CreateCLI.class), + @SubCommand(name = "build-native", impl = BuildNativeCLI.class), + @SubCommand(name = "build-web", impl = BuildWebCLI.class), + @SubCommand(name = "sign", impl = SignCLI.class), + @SubCommand(name = "cli-config", impl = ConfigCLI.class), + @SubCommand(name = "help", impl = HelpCLI.class) + }) + private AbstractCLI tizenCLI; + + /** + * @param args + */ + public static void main(String[] args) { + Main tizen = new Main(); + tizen.run(args); + + } + + private void run(String[] args) { + CommandLineParser cmdParser = new CommandLineParser(this); + log.trace("Start running Tizen CLI Main class..."); + log.trace("Argument count:{}",args.length); + PrintWriter errorWriter = new PrintWriter(System.err); + try { + cmdParser.setUsageWidth(120); + // Argument parsing - make metadata from annotation. + cmdParser.parseArgument(args); + } catch (CmdLineException e) { + if( args.length > 0) { + errorWriter.println(); + errorWriter.println("Argument is not valid!"); + errorWriter.println(MessageFormat.format("Error: {0}", e.getMessage())); + errorWriter.println(); + }else if(args.length == 0){ + errorWriter.println(); + errorWriter.println("Usage: tizen [args]"); + errorWriter.println(); + errorWriter.println("Where is one of "); + //TODO sub command list should be printed by input argument. + } + errorWriter.flush(); + System.exit(1); + } + + try { + //Execute actual command classes + if (null != this.tizenCLI) { + this.tizenCLI.execute(); + } + }catch(Exception e) { + e.printStackTrace(); + } + + errorWriter.flush(); + if( null != tizenCLI.output) { + tizenCLI.output.flush(); + } + System.exit(1); + + } + +} -- 2.7.4