3434
3535#include < boost/algorithm/string.hpp>
3636#include < primitives/pack.h>
37+ #include < primitives/optional.h>
3738
3839#include < iostream>
3940#include < thread>
@@ -54,6 +55,8 @@ void default_run();
5455void init (const Strings &args, const String &log_level);
5556void load_current_config ();
5657void self_upgrade ();
58+ void self_upgrade_copy (const path &dst);
59+ optional<int > internal (const Strings &args);
5760void command_init (const Strings &args);
5861
5962int main (int argc, char *argv[])
111114 args = args_copy;
112115 }
113116
114- // check if single arg is dir name
115- if (args.size () == 2 )
116- {
117- auto cmd = args[1 ];
118-
119- // file/url arg
120- if (!isUrl (cmd) && fs::is_directory (cmd))
121- {
122- cp = std::make_unique<ScopedCurrentPath>(cmd);
123- args.erase (args.end () - 1 );
124- }
125- }
126-
127117 // main cppan client init routine
128118 init (args, log_level);
129119
@@ -134,87 +124,16 @@ try
134124 return 0 ;
135125 }
136126
127+ if (auto r = internal (args))
128+ return r.get ();
129+
137130 if (args.size () > 1 )
138131 {
139- auto self_upgrade = [&args]()
140- {
141- // self upgrade via copy
142- std::this_thread::sleep_for (std::chrono::seconds (1 ));
143- fs::copy_file (args[0 ], args[2 ], fs::copy_option::overwrite_if_exists);
144- };
132+ const auto cmd = args[1 ];
145133
146134 // command selector, always exit inside this if()
147- if (args[ 1 ] [0 ] != ' -' )
135+ if (cmd [0 ] != ' -' )
148136 {
149- String cmd = args[1 ];
150-
151- // internal
152- if (cmd.find (" internal-" ) == 0 )
153- {
154- if (cmd == " internal-fix-imports" )
155- {
156- if (args.size () != 6 )
157- {
158- std::cout << " invalid number of arguments\n " ;
159- std::cout << " usage: cppan internal-fix-imports target aliases.file old.file new.file\n " ;
160- return 1 ;
161- }
162- fix_imports (args[2 ], args[3 ], args[4 ], args[5 ]);
163- return 0 ;
164- }
165-
166- if (cmd == " internal-parallel-vars-check" )
167- {
168- if (args.size () < 6 )
169- {
170- std::cout << " invalid number of arguments: " << args.size () << " \n " ;
171- std::cout << " usage: cppan internal-parallel-vars-check vars_dir vars_file checks_file generator toolset toolchain\n " ;
172- return 1 ;
173- }
174-
175- size_t a = 2 ;
176-
177- #define ASSIGN_ARG (x ) if (a < args.size()) o.x = trim_double_quotes(args[a++])
178- ParallelCheckOptions o;
179- ASSIGN_ARG (dir);
180- ASSIGN_ARG (vars_file);
181- ASSIGN_ARG (checks_file);
182- ASSIGN_ARG (generator);
183- ASSIGN_ARG (toolset);
184- ASSIGN_ARG (toolchain);
185- #undef ASSIGN_ARG
186-
187- CMakePrinter c;
188- c.parallel_vars_check (o);
189- return 0 ;
190- }
191-
192- if (cmd == " internal-create-link-to-solution" )
193- {
194- #ifndef _WIN32
195- return 0 ;
196- #endif
197- if (args.size () != 4 )
198- {
199- std::cout << " invalid number of arguments: " << args.size () << " \n " ;
200- std::cout << " usage: cppan internal-create-link-to-solution solution.sln link.lnk\n " ;
201- return 1 ;
202- }
203- if (!create_link (args[2 ], args[3 ], " Link to CPPAN Solution" ))
204- return 1 ;
205- return 0 ;
206- }
207-
208- if (args[1 ] == " internal-self-upgrade-copy" )
209- {
210- self_upgrade ();
211- return 0 ;
212- }
213-
214- return 0 ;
215- }
216-
217- // normal options
218137 if (cmd == " parse-configure-ac" )
219138 {
220139 if (args.size () != 3 )
@@ -294,11 +213,11 @@ try
294213
295214 std::cout << " unknown command: " << cmd << " \n " ;
296215 return 1 ;
297- }
216+ }
298217#ifdef _WIN32
299218 else if (args[1 ] == " --self-upgrade-copy" ) // remove this very very later (at 0.3.0 - 0.5.0)
300219 {
301- self_upgrade ( );
220+ self_upgrade_copy (args[ 2 ] );
302221 return 0 ;
303222 }
304223#endif
@@ -588,6 +507,78 @@ void self_upgrade()
588507#endif
589508}
590509
510+ void self_upgrade_copy (const path &dst)
511+ {
512+ std::this_thread::sleep_for (std::chrono::seconds (2 ));
513+ fs::copy_file (get_program (), dst, fs::copy_option::overwrite_if_exists);
514+ }
515+
516+ optional<int > internal (const Strings &args)
517+ {
518+ // internal stuff
519+ if (args[1 ] == " internal-fix-imports" )
520+ {
521+ if (args.size () != 6 )
522+ {
523+ std::cout << " invalid number of arguments\n " ;
524+ std::cout << " usage: cppan --internal-fix-imports target aliases.file old.file new.file\n " ;
525+ return 1 ;
526+ }
527+ fix_imports (args[2 ], args[3 ], args[4 ], args[5 ]);
528+ return 0 ;
529+ }
530+
531+ if (args[1 ] == " internal-create-link-to-solution" )
532+ {
533+ #ifndef _WIN32
534+ return 0 ;
535+ #endif
536+ if (args.size () != 4 )
537+ {
538+ std::cout << " invalid number of arguments: " << args.size () << " \n " ;
539+ std::cout << " usage: cppan --internal-create-link-to-solution solution.sln link.lnk\n " ;
540+ return 1 ;
541+ }
542+ if (!create_link (args[2 ], args[3 ], " Link to CPPAN Solution" ))
543+ return 1 ;
544+ return 0 ;
545+ }
546+
547+ if (args[1 ] == " internal-parallel-vars-check" )
548+ {
549+ if (args.size () < 6 )
550+ {
551+ std::cout << " invalid number of arguments: " << args.size () << " \n " ;
552+ std::cout << " usage: cppan --internal-parallel-vars-check vars_dir vars_file checks_file generator toolset toolchain\n " ;
553+ return 1 ;
554+ }
555+
556+ size_t a = 2 ;
557+
558+ #define ASSIGN_ARG (x ) if (a < args.size()) o.x = trim_double_quotes(args[a++])
559+ ParallelCheckOptions o;
560+ ASSIGN_ARG (dir);
561+ ASSIGN_ARG (vars_file);
562+ ASSIGN_ARG (checks_file);
563+ ASSIGN_ARG (generator);
564+ ASSIGN_ARG (toolset);
565+ ASSIGN_ARG (toolchain);
566+ #undef ASSIGN_ARG
567+
568+ CMakePrinter c;
569+ c.parallel_vars_check (o);
570+ return 0 ;
571+ }
572+
573+ if (args[1 ] == " internal-self-upgrade-copy" )
574+ {
575+ self_upgrade_copy (args[2 ]);
576+ return 0 ;
577+ }
578+
579+ return optional<int >();
580+ }
581+
591582ApiResult api_call (const String &cmd, const Strings &args)
592583{
593584 auto us = Settings::get_user_settings ();
0 commit comments