-
Notifications
You must be signed in to change notification settings - Fork 333
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
fixed issues 445, add a method 'addSubcommand' #522
Draft
Asriter
wants to merge
3
commits into
cbeust:master
Choose a base branch
from
Asriter:master1
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
85 changes: 85 additions & 0 deletions
85
src/test/java/com/beust/jcommander/command/subcommandTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
package test; | ||
|
||
import com.beust.jcommander.*; | ||
import java.util.*; | ||
import org.testng.annotations.Test; | ||
import org.testng.Assert; | ||
|
||
class testClass1 { | ||
@Parameter(names = "testCommand",description = "same name test") | ||
public String result = "null"; | ||
|
||
@Parameter(names = "mainTestCommand",description = "main command test") | ||
public int mainResult = -1; | ||
} | ||
|
||
class testClass2 { | ||
@Parameter(names = "testCommand",description = "test 2 over~!") | ||
public String result = "null"; | ||
|
||
@Parameter(names = "subTestCommand",description = "sub command test") | ||
public int subResult = -1; | ||
} | ||
|
||
|
||
////CS304 (manually written) Issue link:https://github.com/cbeust/jcommander/issues/431 | ||
public class subcommandTest { | ||
|
||
@Test | ||
private void subCommandOnlyTest() { | ||
//String[] args = new String[]{"command1", "mainTestCommand", "1", "command1", "sub", "subTestCommand", "2"}; | ||
//String[] args = new String[]{"command1", "mainTestCommand", "1"}; | ||
String[] args = new String[]{"command1", "sub", "subTestCommand", "2"}; | ||
JCommander jc = new JCommander(this); | ||
testClass1 mainCommand = new testClass1(); | ||
testClass2 subCommand = new testClass2(); | ||
jc.addCommand("command1", mainCommand); | ||
jc.addSubcommand("sub", subCommand, "command1"); | ||
jc.parse(args); | ||
|
||
Assert.assertTrue(mainCommand.mainResult == -1, "the result of main command is:" + mainCommand.mainResult + " but not -1"); | ||
Assert.assertTrue(subCommand.subResult == 2, "the result of sub command is:" + subCommand.subResult + " but not 2"); | ||
} | ||
|
||
@Test | ||
private void mainCommandOnlyTest() { | ||
String[] args = new String[]{"command1", "mainTestCommand", "1"}; | ||
JCommander jc = new JCommander(this); | ||
testClass1 mainCommand = new testClass1(); | ||
testClass2 subCommand = new testClass2(); | ||
jc.addCommand("command1", mainCommand); | ||
jc.addSubcommand("sub", subCommand, "command1"); | ||
jc.parse(args); | ||
|
||
Assert.assertTrue(mainCommand.mainResult == 1, "the result of main command is:" + mainCommand.mainResult + " but not 1"); | ||
Assert.assertTrue(subCommand.subResult == -1, "the result of sub command is:" + subCommand.subResult + " but not -1"); | ||
} | ||
|
||
@Test | ||
private void sameNameParamentTestSub() { | ||
String[] args = new String[]{"command1", "sub", "testCommand", "test over"}; | ||
JCommander jc = new JCommander(this); | ||
testClass1 mainCommand = new testClass1(); | ||
testClass2 subCommand = new testClass2(); | ||
jc.addCommand("command1", mainCommand); | ||
jc.addSubcommand("sub", subCommand, "command1"); | ||
jc.parse(args); | ||
|
||
Assert.assertTrue(mainCommand.result == "null", "the result of main command is:" + mainCommand.result + " but not null"); | ||
Assert.assertTrue(subCommand.result == "test over", "the result of sub command is:" + subCommand.result + " but not test over"); | ||
} | ||
|
||
@Test | ||
private void sameNameParamentTestMain() { | ||
String[] args = new String[]{"command1", "testCommand", "test over"}; | ||
JCommander jc = new JCommander(this); | ||
testClass1 mainCommand = new testClass1(); | ||
testClass2 subCommand = new testClass2(); | ||
jc.addCommand("command1", mainCommand); | ||
jc.addSubcommand("sub", subCommand, "command1"); | ||
jc.parse(args); | ||
|
||
Assert.assertTrue(mainCommand.result == "test over", "the result of main command is:" + mainCommand.result + " but not test over"); | ||
Assert.assertTrue(subCommand.result == "null", "the result of sub command is:" + subCommand.result + " but not null"); | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I do not understand the benefit of this command over the existing possibility of simply calling
parentCommand.addCommand()
directly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asriter Unfortunately you have no responded to my question, so I hope you do not feel discouraged from setting the state of this PR to draft.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Asriter Kindly requsting your response! :-)