制作'干净'要求确认

Ban*_*nex 12 makefile cmake

有没有办法让命令make clean需要用户确认?我错误地执行了它,现在我必须等待6个小时才能再次完成构建.

Makefile小号被创建cmake.

期望的工作流程:

> make clean
> [make] Are you sure you want to remove all the built files? [Y/N]
> N
> [make] Target 'make clean' not executed.

> make clean
> [make] Are you sure you want to remove all the built files? [Y/N]
> Y
> [make] Target 'make clean' executed.
Run Code Online (Sandbox Code Playgroud)

Har*_*nry 17

我不熟悉cmake,但对于gnu make,一个可能的hack将是:

clean: check_clean

check_clean:
    @echo -n "Are you sure? [y/N] " && read ans && [ $${ans:-N} = y ]

.PHONY: clean check_clean
Run Code Online (Sandbox Code Playgroud)

如果check_clean失败(用户未输入y),则make将在执行清除之前退出并显示错误.

  • 几点改进:``@echo -n"你确定吗?[y/N]"&&读ans && [$$ {ans:-N} == y]`` (2认同)
  • 我正在使用case:`@(读-p“你确定吗?!?[y / N]:” sure && case“ $$ sure” in [yY])true ;; *)错误;esac)` (2认同)
  • @HardcoreHenry:你确定它是“[ $${ans:-N} == y ]”而不是“[ $${ans:-N} = y ]”?使用您的命令时,我的 Makefile 在键入任何内容时都会失败,但是当我更改为单个等号字符时,我工作得很好。 (2认同)
  • 两者都对我有用,但我只是查了一下,显然 `==` 不符合 Posix 标准,所以可能不适用于某些 shell。我将更新答案以使用单个“=”。谢谢。 (2认同)