1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 00:17:46 +00:00

Userland+Terminal: Port to new CArgsParser API

While at it, also add some niceties and fix some things.
This commit is contained in:
Sergey Bugaev 2020-01-27 20:25:36 +03:00 committed by Andreas Kling
parent 9276582535
commit f983dfe319
22 changed files with 392 additions and 653 deletions

View file

@ -24,8 +24,8 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/String.h>
#include <AK/FileSystemPath.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <LibCore/CArgsParser.h>
#include <LibCore/CDirIterator.h>
@ -46,27 +46,28 @@ int main(int argc, char** argv)
return 1;
}
CArgsParser args_parser("cp");
args_parser.add_arg("r", "copy directories recursively");
args_parser.add_required_single_value("source");
args_parser.add_required_single_value("destination");
bool recursion_allowed = false;
Vector<const char*> sources;
const char* destination = nullptr;
CArgsParserResult args = args_parser.parse(argc, argv);
Vector<String> values = args.get_single_values();
if (values.size() == 0) {
args_parser.print_usage();
return 0;
CArgsParser args_parser;
args_parser.add_option(recursion_allowed, "Copy directories recursively", "recursive", 'r');
args_parser.add_positional_argument(sources, "Source file path", "source");
args_parser.add_positional_argument(destination, "Destination file path", "destination");
args_parser.parse(argc, argv);
for (auto& source : sources) {
bool ok = copy_file_or_directory(source, destination, recursion_allowed);
if (!ok)
return 1;
}
bool recursion_allowed = args.is_present("r");
String src_path = values[0];
String dst_path = values[1];
return copy_file_or_directory(src_path, dst_path, recursion_allowed) ? 0 : 1;
return 0;
}
/**
* Copy a file or directory to a new location. Returns true if successful, false
* Copy a file or directory to a new location. Returns true if successful, false
* otherwise. If there is an error, its description is output to stderr.
*
*
* Directories should only be copied if recursion_allowed is set.
*/
bool copy_file_or_directory(String src_path, String dst_path, bool recursion_allowed)
@ -95,9 +96,9 @@ bool copy_file_or_directory(String src_path, String dst_path, bool recursion_all
}
/**
* Copy a source file to a destination file. Returns true if successful, false
* Copy a source file to a destination file. Returns true if successful, false
* otherwise. If there is an error, its description is output to stderr.
*
*
* To avoid repeated work, the source file's stat and file descriptor are required.
*/
bool copy_file(String src_path, String dst_path, struct stat src_stat, int src_fd)