2424public class PasteCommand extends AbstractCommand {
2525 private static final Logger logger = LoggerFactory .getLogger (SubmitCommand .class );
2626
27+ private CliContext ctx ;
2728 private Io io ;
2829
30+ String message ;
31+ boolean noMessage ;
32+ boolean openInBrowser ;
33+
34+ @ Override
35+ public String [] getUsages () {
36+ return new String [] {"tmc paste [-o] [-n] [-m MESSAGE] [EXERCISE]" };
37+ }
38+
2939 @ Override
3040 public void getOptions (Options options ) {
3141 options .addOption ("n" , "no-message" , false , "Don't send a message with your paste" );
@@ -35,40 +45,52 @@ public void getOptions(Options options) {
3545
3646 @ Override
3747 public void run (CliContext context , CommandLine args ) {
38- CliContext ctx = context ;
48+ this . ctx = context ;
3949 this .io = ctx .getIo ();
40- if (!ctx .loadBackend ()) {
41- return ;
42- }
50+
51+ this .message = args .getOptionValue ("m" );
52+ this .noMessage = args .hasOption ("n" );
53+ this .openInBrowser = args .hasOption ("o" );
4354 WorkDir workdir = ctx .getWorkDir ();
4455 String [] stringArgs = args .getArgs ();
4556
46- Boolean valid ;
47- if (stringArgs .length == 0 ) {
48- valid = workdir .addPath ();
49- } else if (stringArgs .length == 1 ) {
50- valid = workdir .addPath (stringArgs [0 ]);
51- } else {
52- io .errorln ("Error: Too many arguments. Expected 1, got " + stringArgs .length );
57+ if (noMessage && message != null ) {
58+ io .errorln ("You can't have the no-message flag and message set at the same time." );
59+ printUsage (context );
60+ return ;
61+ }
62+ if (stringArgs .length > 1 ) {
63+ io .errorln ("Error: Too many arguments." );
64+ printUsage (context );
5365 return ;
5466 }
55- if (!valid ) {
56- io .errorln ("The command can be used in an exercise directory without the exercise name"
57- + " or in a course directory with the name as an argument." );
67+
68+ if (stringArgs .length == 1 ) {
69+ if (!workdir .addPath (stringArgs [0 ])) {
70+ io .errorln ("The path '" + stringArgs [0 ] + "' is not valid exercise." );
71+ return ;
72+ }
73+ } else {
74+ //TODO replace the following call with workdir.getCurrentExercise()
75+ if (!workdir .addPath ()) {
76+ io .errorln ("You are not in exercise directory." );
77+ return ;
78+ }
79+ }
80+
81+ if (!ctx .loadBackend ()) {
5882 return ;
5983 }
6084
61- List <String > exercisenames = workdir .getExerciseNames ();
62- if (exercisenames .size () != 1 ) {
85+ List <String > exerciseNames = workdir .getExerciseNames ();
86+ if (exerciseNames .size () != 1 ) {
6387 io .errorln ("Error: Matched too many exercises." );
88+ printUsage (context );
6489 return ;
6590 }
6691
67- String message = "" ;
68- if (!args .hasOption ("n" )) {
69- if (args .hasOption ("m" )) {
70- message = args .getOptionValue ("m" );
71- } else if (io .readConfirmation ("Attach a message to your paste?" , true )) {
92+ if (!noMessage && message == null ) {
93+ if (io .readConfirmation ("Attach a message to your paste?" , true )) {
7294 message = ExternalsUtil .getUserEditedMessage (
7395 "\n "
7496 + "# Write a message for your paste in this file and save it.\n "
@@ -79,10 +101,17 @@ public void run(CliContext context, CommandLine args) {
79101 true );
80102 }
81103 }
104+ sendPaste (message , exerciseNames .get (0 ));
105+ }
106+
107+ private void sendPaste (String message , String exerciseName ) {
108+ if (message == null ) {
109+ message = "" ;
110+ }
82111
83- String exerciseName = exercisenames .get (0 );
84112 CourseInfo info = ctx .getCourseInfo ();
85113 Exercise exercise = info .getExercise (exerciseName );
114+
86115 URI uri = TmcUtil .sendPaste (ctx , exercise , message );
87116 if (uri == null && exercise .hasDeadlinePassed ()) {
88117 io .errorln ("Unable to send the paste."
@@ -96,7 +125,7 @@ public void run(CliContext context, CommandLine args) {
96125
97126 io .println ("Paste sent for exercise " + exercise .getName ());
98127 io .println (uri .toString ());
99- if (args . hasOption ( "o" ) ) {
128+ if (openInBrowser ) {
100129 ExternalsUtil .openInBrowser (uri );
101130 }
102131 }
0 commit comments