在本文中复刻了 cp
实用程序的功能,我想默认使其递归,因为每次我想复制时都输入 -R
文件夹都会觉得有点重复,本文代码将与前文代码保持相似,我们只会更改程序的核心功能和一些变量名称以匹配用例
Pseudo Code 伪代码
function copy(src,dst)->result{
create dst directory
for entry in src{
get file_type
if file_type.is_dir(){
copy(entry.path(), dst.as_ref().join(entry.file_name()))?
} else {
fs::copy(entry.path(), dst.as_ref().join(entry.file_name()))?
}
}
}
args = command_line_arguments
remove the first element of the args vector
if args.length == 0 {
print error_message
}
else if args.contains("--help") {
print help_message
} else {
result = copy(args[0],args[1])
error handling
}
Looks pretty simple, we’ll create a lib.rs and do the basic setup tasks just like yesterday. We’ll create the following :
看起来很简单,我们将创建一个 lib.rs 并像昨天一样执行基本设置任务。我们将创建以下内容:
config
Structconfig
结构体- An implementation block 一个实现块
new
methodnew
方法help
methodhelp
方法not_enough_arguments
methodnot_enough_arguments
方法run
functionrun
函数
and then we’ll write the logic for our copy
function
然后我们将为 copy
函数编写逻辑