Claude Codeを使ってみた
はじめに
Claude Codeとは、ターミナル上で動作し、コードベースを理解し、自然言語による指示を通じてより速くコーディングを支援するエージェント型コーディングツールです。開発環境に直接統合することで、追加のサーバーや複雑なセットアップを必要とせずにワークフローを効率化します。
Claude Codeの概要は以下のブログにClaude 3.7 Sonnetと共に簡単にまとめています。
セットアップ
- Claud Codeのインストール
npm install -g @anthropic-ai/claude-code
claudeコマンドがインストールされました。
% claude -v
0.2.19
- claudeコマンドの実行
claudeコマンドのセットアップをしていきます。
textのスタイルを聞かれます。
リサーチ版でいくつかの注意事項があることを確認していきます。
% claude
╭────────────────────────────────────────────╮
│ ✻ Welcome to Claude Code research preview! │
│ │
│ /help for help │
│ │
│ cwd: /Users/hogehoge/fugafuga/piyopiyo │
╰────────────────────────────────────────────╯
Tips for getting started:
1. Ask Claude to create a new app or clone a repository.
2. Run /terminal-setup to set up terminal integration
3. Ask Claude questions about your codebase.
4. Ask Claude to implement changes to your codebase.
╭──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╮
│ > Try "fix lint errors" │
╰──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────╯
! for bash mode · / for commands · esc to undo \⏎ for newline
このような画面がclaudeコマンドを実行して出てくればセットアップが完了です。
始めるためのヒント
- クロードに新しいアプリを作るか、リポジトリをクローンしてもらう。
- ターミナル統合をセットアップするために/terminal-setupを実行する。
- コードベースについてクロードに質問する。
- あなたのコードベースに変更を加えるようクロードに依頼する。
改行について
Tip for iTerm2 and VSCode users: Run /terminal-setup within Claude Code to automatically configure Shift+Enter as a more intuitive alternative.
shift + Enterで改行できるようになるので /terminal-setup
をしておくと良いです。
実践
hello world
まずはプログラムの初心でhello worldを表示するだけのアプリを書いてもらいます。
そして最後にこのファイルの修正実施してもよい?と聞かれるのでそれに答えればプログラムが保存されます。
そして完成したのがこちら
print("Hello World")
ものすごくシンプルですが正しく動作しそうですね。
これぞHello Worldといった感じです。
プログラムの修正
先ほど作ったファイルに修正をかけていきたいと思います。
まずはこのような指示を
まずは1秒ごとにHello worldと表示するようにしてください、
次に5回ごとにgood byeと表示するようにしてください。
30秒経過したらプログラムを終了してください
そして完成したプログラムがこちら
完成したプログラム
import time
count = 0
start_time = time.time()
while True:
print("Hello World")
count += 1
if count % 5 == 0:
print("good bye")
# 30秒経過したらプログラムを終了
if time.time() - start_time >= 30:
print("Program finished after 30 seconds")
break
time.sleep(1)
これも愚直に実装したなという感じですね。
確かに動作しそうですがどことなくイけてないですよね。
そこでclaudeに雑にリファクタリングしてと依頼してみます。
そしてリファクタリングの結果がこちら...
リファクタしたプログラム
import time
from typing import Optional
def print_message(count: int) -> None:
"""Print appropriate message based on iteration count.
Args:
count: Current iteration count
"""
print("Hello World")
if count % 5 == 0:
print("good bye")
def run_timer(duration_seconds: Optional[int] = 30) -> None:
"""Run timer that prints messages for specified duration.
Args:
duration_seconds: How long to run in seconds
"""
count = 0
start_time = time.time()
while True:
count += 1
print_message(count)
if time.time() - start_time >= duration_seconds:
print(f"Program finished after {duration_seconds} seconds")
break
time.sleep(1)
if __name__ == "__main__":
run_timer()
メソッドに分かれてコメントも追加されましたね。
リファクタリングしてとの指示だけでこれぐらいの修正が実施されました。
プログラムの説明
次は修正ではなく説明をしてもらいます。
今回は先ほど作成したプログラムについて説明してもらいます。
hello.py
はコピーして残しておきました。
このファルダのプログラムを説明して
ちゃんと中身までみてどのような意図のプログラムなのかを説明してくれます。
補足
claude --help
を実行するとこのようなヘルプページが表示されます。
どういったことができるのかという説明がされています。
docsにも詳細な説明が載っていますのでそちらを参照されても良いかと思います。
claude --helpの結果
% claude --help
Usage: claude [options] [command] [prompt]
Claude Code - starts an interactive session by default, use -p/--print for non-interactive output
Slash commands available during an interactive session:
/clear - Clear conversation history and free up context
/compact - Clear conversation history but keep a summary in context
/config - Open config panel
/cost - Show the total cost and duration of the current session
/doctor - Checks the health of your Claude Code installation
/help - Show help and available commands
/init - Initialize a new CLAUDE.md file with codebase documentation
/pr-comments - Get comments from a GitHub pull request
/bug - Submit feedback about Claude Code
/review - Review a pull request
/terminal-setup - Install Shift+Enter key binding for newlines (iTerm2 and VSCode only)
/logout - Sign out from your Anthropic account
/login - Switch Anthropic accounts
Arguments:
prompt Your prompt
Options:
-c, --cwd <cwd> The current working directory (default: "/Users/hogehoge/fugafuga")
-d, --debug Enable debug mode
--verbose Override verbose mode setting from config
-ea, --enable-architect Enable the Architect tool
-p, --print Print response and exit (useful for pipes)
--dangerously-skip-permissions Skip all permission checks. Only works in Docker containers with no internet access. Will crash otherwise.
-v, --version output the version number
-h, --help display help for command
Commands:
config Manage configuration (eg. claude config set -g theme dark)
mcp Configure and manage MCP servers
doctor Check the health of your Claude Code auto-updater
実際に一通り目を通してみることをお勧めします。
注意点
Supported shells:
Bash
Zsh (Fish shell not currently supported)
このように記載がありZshがサポートされていませんのでmacOSなどをお使いの場合は注意が必要です。
私もclaudeコマンドで作成したpythonファイルを実行させようとしましたが、うまく動きませんでした。
ここでyesを選択しても返答が返ってきません。
デフォルトのシェルをzshからbashに変更すると動く可能性もありますが、今回はそこまで検証は行なっておりません。
2025/03/01 追記
claudeコマンドを実行時に
env SHELL=/bin/bash claude
のようにShellを指定してコマンドを実行するとこの問題は解決するようです。
おわりに
ドキュメントが充実しているということは素晴らしいですね。
ただしresearch preview版なのでバグや不具合が多いため動作には注意が必要です。
ターミナル上でAIと対話しながら開発ができるのは新しい体験で今後がとても楽しみです。
今回はまだ試せていませんが、mcpサーバーの構成設定などもできそうなので色々と使い道はあり可能性を感じました。
今後のアップデートがとても楽しみです。
Discussion