텍스트 대소문자를 변경하는 사용자 정의 터미널 기능

텍스트 대소문자를 변경하는 사용자 정의 터미널 기능

change_case내 웹사이트에서 내 타이틀을 관리하는 데 도움이 되도록 아래와 같이 작동하는 사용자 정의 터미널 기능을 찾고 있습니다 .

change_case [option] "string"

option:
    upper - STRING TO UPPERCASE
    lower - string to lowercase
    sentence - Uppercase first letter of every word, others to lowercase
    custom - String to Sentence Case except These Words if they appear as the 1st letter:
        [in,by,with,of,a,to,is,and,the]

예시 제목 -자동으로 로그인하는 대신 로그인 화면을 표시하려면 어떻게 해야 합니까?

높은:자동으로 로그인하는 대신 로그인 화면을 표시하려면 어떻게 해야 합니까?

낮추다:자동으로 로그인하는 대신 로그인 화면을 표시하려면 어떻게 해야 하나요?

문장:자동으로 로그인하는 대신 로그인 화면이 나타나도록 하려면 어떻게 해야 합니까?

관습:자동으로 로그인하는 대신 로그인 화면을 표시하려면 어떻게 해야 합니까?

답변1

너무 복잡하지는 않습니다.

  1. 아래 스크립트를 빈 파일에 복사하고 (디렉토리를 만들어야 할 수도 있음) change_case에 (확장자 없음) 으로 저장합니다.~/bin스크립트를 실행 가능하게 만들기
  2. 특히 디렉터리가 아직 존재하지 않는 경우 로그아웃/로그인해야 할 수도 있습니다(또는 다음을 실행 source ~/.profile) .
  3. 터미널 창을 열고 다음 명령을 실행하여 테스트합니다.

    change_case custom this is a test case to see if all in the script works
    

    산출:

    This is a Test Case to See If All in the Script Works
    

귀하의 질문에 있는 모든 옵션(상위, 하위, 문장, 사용자 정의)을 사용하여 테스트했으며 모두 귀하의 예시로 작동할 것입니다.

스크립트

#!/usr/bin/env python3
import sys

string = sys.argv[2:]
opt = sys.argv[1]

excluded = ["in","by","with","of","a","to","is","and","the"]

if opt == "lower":
    print((" ").join(string).lower())
elif opt == "upper":
    print((" ").join(string).upper())
elif opt == "sentence":
    print((" ").join(string).title())
elif opt == "custom":
    line = []
    for s in string:
        s = s.title() if not s in excluded else s
        line.append(s)
    print((" ").join(line))

관련 정보