Skip to content

Commit 104ab4e

Browse files
author
David Hooker
committed
Initial code commit
1 parent 3b49295 commit 104ab4e

32 files changed

+3175
-0
lines changed

.classpath

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" output="target/classes" path="src/main/java"/>
4+
<classpathentry kind="src" output="target/test-classes" path="src/test/java"/>
5+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4"/>
6+
<classpathentry kind="con" path="org.maven.ide.eclipse.MAVEN2_CLASSPATH_CONTAINER"/>
7+
<classpathentry kind="output" path="target/classes"/>
8+
</classpath>

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
target/
2+
.settings/
3+

.project

+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>cliche2</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
<buildCommand>
14+
<name>org.maven.ide.eclipse.maven2Builder</name>
15+
<arguments>
16+
</arguments>
17+
</buildCommand>
18+
</buildSpec>
19+
<natures>
20+
<nature>org.eclipse.jdt.core.javanature</nature>
21+
<nature>org.maven.ide.eclipse.maven2Nature</nature>
22+
</natures>
23+
</projectDescription>

pom.xml

+49
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>net.dudehook</groupId>
6+
<artifactId>cliche2</artifactId>
7+
<version>0.0.1-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>cliche2</name>
11+
12+
<properties>
13+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
14+
</properties>
15+
16+
<description>Interactive command shell library for Java</description>
17+
<scm>
18+
<developerConnection>David Hooker</developerConnection>
19+
<url>[email protected]:dudehook/Cliche2.git</url>
20+
</scm>
21+
22+
<build>
23+
<resources>
24+
<resource>
25+
<directory>src/main/resources</directory>
26+
</resource>
27+
</resources>
28+
<plugins>
29+
<plugin>
30+
<groupId>org.apache.maven.plugins</groupId>
31+
<artifactId>maven-compiler-plugin</artifactId>
32+
<version>2.1</version>
33+
<configuration>
34+
<source>1.6</source>
35+
<target>1.6</target>
36+
</configuration>
37+
</plugin>
38+
</plugins>
39+
</build>
40+
41+
<dependencies>
42+
<dependency>
43+
<groupId>junit</groupId>
44+
<artifactId>junit</artifactId>
45+
<version>3.8.1</version>
46+
<scope>test</scope>
47+
</dependency>
48+
</dependencies>
49+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
/*
2+
* This file is part of the Cliche project, licensed under MIT License. See LICENSE.txt file in root folder of Cliche
3+
* sources.
4+
*/
5+
6+
package net.dudehook.cliche2;
7+
8+
/**
9+
* Root exception for Cliche.
10+
*
11+
* @author ASG
12+
*/
13+
public class CLIException extends Exception
14+
{
15+
public CLIException()
16+
{
17+
super();
18+
}
19+
20+
public CLIException(String message)
21+
{
22+
super(message);
23+
}
24+
25+
public CLIException(Throwable cause)
26+
{
27+
super(cause);
28+
}
29+
30+
public CLIException(String message, Throwable cause)
31+
{
32+
super(message, cause);
33+
}
34+
35+
public static CLIException createCommandNotFound(String commandName)
36+
{
37+
return new CLIException("Unknown command: " + Token.escapeString(commandName));
38+
}
39+
40+
public static CLIException createCommandNotFoundForArgNum(String commandName, int argCount)
41+
{
42+
return new CLIException("There's no command " + Token.escapeString(commandName) + " taking " + argCount + " arguments");
43+
}
44+
45+
public static CLIException createAmbiguousCommandExc(String commandName, int argCount)
46+
{
47+
return new CLIException("Ambiguous command " + Token.escapeString(commandName) + " taking " + argCount + " arguments");
48+
}
49+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* This file is part of the Cliche project, licensed under MIT License.
3+
* See LICENSE.txt file in root folder of Cliche sources.
4+
*/
5+
6+
package net.dudehook.cliche2;
7+
8+
import java.lang.annotation.ElementType;
9+
import java.lang.annotation.Retention;
10+
import java.lang.annotation.RetentionPolicy;
11+
import java.lang.annotation.Target;
12+
13+
/**
14+
* Annotation for commands. Allows to specify the name of a command, otherwise method's name is used.
15+
* @author ASG
16+
*/
17+
@Target(ElementType.METHOD)
18+
@Retention(RetentionPolicy.RUNTIME)
19+
public @interface Command {
20+
/**
21+
* Allows to override default command name, which is derived from method's name
22+
* @return "" or null if default name is used, user-specified name otherwise.
23+
*/
24+
String name() default ""; // if "" then Null is assumed.
25+
26+
/**
27+
* Specify the description of the command. Default description (if this
28+
* property is not set) says "methodName(Arg1Type, Arg2Type,...) : ReturnType".
29+
* @return command's description or "" if not set.
30+
*/
31+
String description() default "";
32+
33+
/**
34+
* Specify the shortcut name for the command.
35+
* If not set, if the name attribute is not set as well, the Shell takes
36+
* the first letter of each word (void selectUser() --- select-user --- su).
37+
* @return command's abbreviation or "" if not set.
38+
*/
39+
String abbrev() default "";
40+
41+
/**
42+
* Specify the string to output before command's output, i.e. some explanations.
43+
* @return command's header or "" if not set.
44+
*/
45+
String header() default "";
46+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* This file is part of the Cliche project, licensed under MIT License. See LICENSE.txt file in root folder of Cliche
3+
* sources.
4+
*/
5+
6+
package net.dudehook.cliche2;
7+
8+
import java.lang.reflect.Method;
9+
import java.util.Arrays;
10+
11+
/**
12+
* This interface is a Strategy for auto-naming commands with no name specified based on command's method. The algorithm
13+
* is isolated because it's highly subjective and therefore subject to changes. And, if you don't like default
14+
* dash-joined naming you can completely redefine the functionality.
15+
*
16+
* It is also responsible for generating several suggested abbreviations, one from them may later be "approved" by
17+
* CommandTable.
18+
*
19+
* @author ASG
20+
*/
21+
public interface CommandNamer
22+
{
23+
24+
/**
25+
* Generate command name and suggested abbreviation variants. Since this method is given a Method, not a string, the
26+
* algorithm can take parameter types into account.
27+
*
28+
* @param commandMethod
29+
* Command method
30+
* @return asg.cliche.CommandNamer.NamingInfo containing generated name and abbrev array.
31+
*/
32+
NamingInfo nameCommand(Method commandMethod);
33+
34+
/**
35+
* Return value grouping structure for nameCommand(). I decided to return name and abbreviations together because in
36+
* the default algorithm they are generated simultaneously, and I think this approach is better than having a
37+
* stateful strategy.
38+
*/
39+
public static class NamingInfo
40+
{
41+
public final String commandName;
42+
public final String[] possibleAbbreviations;
43+
44+
public NamingInfo(String commandName, String[] possibleAbbreviations)
45+
{
46+
this.commandName = commandName;
47+
this.possibleAbbreviations = possibleAbbreviations;
48+
}
49+
50+
@Override
51+
public boolean equals(Object obj)
52+
{
53+
if (obj == null)
54+
{
55+
return false;
56+
}
57+
if (getClass() != obj.getClass())
58+
{
59+
return false;
60+
}
61+
final NamingInfo other = (NamingInfo)obj;
62+
if ((this.commandName == null) ? (other.commandName != null) : !this.commandName.equals(other.commandName))
63+
{
64+
return false;
65+
}
66+
if (this.possibleAbbreviations != other.possibleAbbreviations
67+
&& (this.possibleAbbreviations == null || !Arrays.equals(this.possibleAbbreviations, other.possibleAbbreviations)))
68+
{
69+
return false;
70+
}
71+
return true;
72+
}
73+
74+
@Override
75+
public int hashCode()
76+
{
77+
int hash = 5;
78+
hash = 59 * hash + (this.commandName != null ? this.commandName.hashCode() : 0);
79+
hash = 59 * hash + (this.possibleAbbreviations != null ? Arrays.hashCode(this.possibleAbbreviations) : 0);
80+
return hash;
81+
}
82+
83+
@Override
84+
public String toString()
85+
{
86+
return String.format("NamingInfo(%s, %s)", commandName, Arrays.toString(possibleAbbreviations));
87+
}
88+
}
89+
90+
}

0 commit comments

Comments
 (0)