@@ -15,9 +15,8 @@ public class CommandBuilder {
1515 private final ArrayList <String > cmdArgs , cmdOptions , finalCommand ;
1616
1717 // private static final String WHITE_SPACE = " ";
18- private static final String COMMA = "," ;
1918 private static final String EMPTY_STRING = "" ;
20- private static final Pattern QUOTES_PATTERN = Pattern .compile ("([^\" ]\\ S*|\" .+?\" )\\ s*" );
19+ private static final Pattern QUOTES_PATTERN = Pattern .compile ("([^\" |^' ]\\ S*|[ \" |'] .+?[ \" |'] )\\ s*" );
2120 // ============================================================
2221
2322 public CommandBuilder () {
@@ -78,8 +77,8 @@ public CommandBuilder withArgs(String... args) {
7877
7978 public Command build () {
8079
81- String executableCmdLine = finalCmdList (). toString (). replace ( COMMA , EMPTY_STRING );
82- executableCmdLine = executableCmdLine .substring (1 , executableCmdLine .length () - 1 );
80+ String executableCmdLine = finalCmdLine ( finalCmdList ());
81+ executableCmdLine = executableCmdLine .substring (0 , executableCmdLine .length () - 1 );
8382 String [] executableCmd = splitCmd (executableCmdLine );
8483
8584 return new CommandImpl (executableCmdLine , executableCmd );
@@ -93,14 +92,27 @@ private ArrayList<String> finalCmdList() {
9392
9493 return finalCommand ;
9594 }
95+
96+ private String finalCmdLine (ArrayList <String > cmdList ){
97+ final StringBuilder cmd = new StringBuilder ();
98+ for (String segment : cmdList ) {
99+ cmd .append (segment );
100+ cmd .append (' ' );
101+ }
102+ return cmd .toString ();
103+ }
96104 // ============================================================
97105
98106 private static String [] splitCmd (String cmd ) {
99107 List <String > strings = new ArrayList <>();
100108 Matcher m = QUOTES_PATTERN .matcher (cmd );
101- while (m .find ())
102- strings .add (m .group (1 ));
103- return strings .toArray (new String [strings .size ()]);
109+ while (m .find ()) {
110+ String token = m .group (1 );
111+ token = token .startsWith ("'" ) || token .startsWith ("\" " ) ?
112+ token .replace ("'" , EMPTY_STRING ).replace ("\" " , EMPTY_STRING ) : token ;
113+ strings .add (token );
114+ }
115+ return strings .toArray (new String [0 ]);
104116 }
105117
106118 private void clearAll () {
0 commit comments