Early in the week, the seemingly escalating spate of violence between the United States and Iran had the potential to verge into war. For many, it recalled the intense conflict between those countries in the 1980s. But that conflict offers hope: By following Ronald Reagan’s example, there is a path for avoiding a cycle of ever-escalating tit-for-tat attacks and reprisals. Over three decades ago, Reagan dealt with Iran firmly, but in a measured way, to preserve access to Middle Eastern oil while avoiding general war. Reagan’s lessons in leadership would benefit President Trump.

Hostilities between Iran and Iraq erupted in September 1980, when Iraq’s secularist prime minister, Saddam Hussein, sensed weakness and instability in Iran and ordered a massive invasion. Hussein wanted to capture territory and oil assets and to stymie Iran’s declared intent to spread Islamist fundamentalism across the Arab world. Although Iraq initially occupied 10,000 square miles of Iranian territory, Iran launched a counteroffensive that by 1982 had liberated its own land and reached into Iraq.

When Iran rejected a truce proposed by Hussein, the Iraqi leader widened the war to include attacks on oil tankers, which were followed by both sides bombing civilian population centers. A series of major offensives by ground troops also failed to break the stalemate. By 1988, the two countries had suffered more than 1 million casualties.

While originally neutral in this conflict, the Reagan administration gradually sided with Iraq. Although Iran had been an ally before the 1979 revolution that deposed the Shah and Hussein was a ruthless dictator, officials in Washington came to fear an Iranian military triumph as Ayatollah Ruhollah Khomeini’s armies gained momentum. Since Khomeini’s theocratic regime had replaced the Shah, Iranian agents had fomented revolutionary activity in Saudi Arabia, Kuwait, Bahrain and North Yemen and backed the radical groups attacking U.S. forces and kidnapping U.S. nationals in Lebanon. Reagan feared a hostile Iran spreading anti-Western ideology across the region and gaining control of a disproportionate amount of the Middle East’s oil — access to which he considered a vital national interest.

“Because of the real and psychological impact of a curtailment in the flow of oil from the Persian Gulf on the international economic system,” the president declared in 1983, “we must assure our readiness to deal promptly with actions aimed at disrupting that traffic.” He ordered U.S. national security agencies to consult with other powers about joint security patrols to protect the oil infrastructure in the Gulf, and specifically to defeat Iranian threats to close the Straits of Hormuz.

Reagan bolstered Hussein with economic aid and military supplies, restored diplomatic relations with Iraq in November 1984 after a break of 17 years and organized an international embargo on arms supply to Iran (which the administration briefly violated during the Iran-contra affair in 1985-86).

In 1986, after Iran widened its assault beyond Iraq’s borders and began preparing for a major offensive, Reagan’s approach became starkly anti-Iranian, applying a mixture of diplomacy and firmness to end the war and protect U.S. oil interests. The following year, the United States secured a unanimous resolution of the U.N. Security Council calling for a cease-fire — which Iraq was willing to accept — and imposing sanctions against non-compliers. However, Khomeini refused to accept such terms unless Iraq was identified as the aggressor and assessed penalties for reparations, and he instead escalated the war by firing missiles on Iraqi cities and ordering epic ground offensives.

To pressure Iran, Reagan arranged the reflagging of Kuwaiti tankers — which conveyed Iraqi oil to western markets, making them targets for Iranian attacks — as U.S.-flagships and then ordered the Navy to escort them through the Persian Gulf. Iran attacked the U.S.-flag tankers with speedboat assaults and marine mines, prompting a determined response from Washington. Before long, the U.S. and Iranian navies engaged in firefights. Major clashes ensued from July to October 1987 and April to July 1988, as U.S. forces struck at Iranian minelayers, speedboats, oil platforms and frigates. Tragedy capped the violence on July 3, 1988, when the USS Vincennes, following a clash with Iranian naval vessels, accidentally downed an Iranian civilian airliner flying overhead, killing 290 passengers and crew.

Two weeks after the tragedy, Iran blinked, accepting the U.N. cease-fire resolution. The financial and human costs of the war — the latter accentuated by the airliner disaster — had exceeded Khomeini’s tolerance, and Iranian public morale had waned as Iraq regained the military initiative, fired missiles at Iranian cities and used poison gas on Iranian soldiers. The decision to make peace was “in the best interests of the revolution,” Khomeini declared to his people, although it “is more lethal to me than poison.”

Reagan’s handling of Iran offers several crucial lessons for Trump today. The 40th president cogently articulated the U.S. strategic and economic imperative — the steady supply of oil — worth defending through diplomacy and military action. In accordance with this goal, he shifted the U.S. relationship with Iraq from rivalry to partnership, considering the deeply flawed Hussein regime as the lesser of two evils and a dike against Iranian expansionism.

Nonetheless, Reagan shied away from preemptive attacks and asymmetrical retaliations, instead calibrating tactical moves to address immediate threats while preserving access to escape hatches and avoiding all-out war. When Khomeini sipped the “poison” of cease-fire, finally, Reagan halted the pressure tactics and refrained from gloating. In fact, the U.S. government eventually acknowledged its regret over the destruction of the civilian airliner and paid $62 million in reparations to the families of the dead.

The Trump administration would be wise to ponder these features of Reagan’s legacy: a clear strategic vision and calculated military action to meet specific political and tactical aims.

A prospective war would benefit neither side. Iran seemingly acknowledged that with its relatively measured retaliation for the assassination of Maj. Gen. Qasem Soleimani — missile attacks on U.S. bases that avoided U.S. casualties. Now Trump must follow Reagan’s path and respond in kind. Strategic restraint can save lives and money while bolstering American security.